2017-02-22 62 views
-2

考慮可能是最簡單的代碼:如何自動化Swing對話框上的響應?

//getDialogResponse() cannot be changed. 
public static int getDialogResponse() { 
    return JOptionPane.showConfirmDialog(null, "Make a really important choice.", "THE CHOICE", JOptionPane.YES_NO_CANCEL_OPTION); 
} 

public static void main(String[] args) { 
    int[] val = new int[1]; 
    SwingUtilities.invokeLater(() -> {val[0] = getDialogResponse();}); 
    //TODO: The program itself presses the 'Yes' button. 
    if(val[0] == JOptionPane.YES_OPTION) System.out.println("Good Choice."); 
    else System.out.println("Wise guy, huh?!"); 
} 

Robot似乎不合身,因爲它需要知道新創建的對話框的位置,如果每次是不是在同一個位置,它將無法工作,所以我需要知道如何獲取按鈕本身的位置,或者我需要知道不依賴於新創建的對話框位置的方法。

此外,我不能更改getDialogResponse()代碼:這是一個不變量。

那麼我該怎麼辦?

+0

爲什麼在不能點擊的情況下首先顯示面板? –

+0

@MariusK。這是用於單元測試的目的。通常情況下,用戶會* *能夠點擊它,但我需要一段代碼來自動點擊按鈕對我來說,這樣的測試可以驗證所產生的行爲是正確的。 – Xirema

+0

也許這可能有助於http://stackoverflow.com/questions/2730044/closing-a-dialog-created-by-joptionpane-showoptiondialog – Massimo

回答

0

Sikuli支持查找對話框/按鈕並以自動方式點擊它們。也許它會幫助你。