【问题标题】:How can I automate the response on a Swing Dialog Box?如何自动响应 Swing 对话框?
【发布时间】:2017-02-23 06:02:58
【问题描述】:

给出最简单的代码:

//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() 代码:这是一个不变量。

那我该怎么办?

【问题讨论】:

  • 如果无法点击,为什么首先显示面板?
  • @MariusK。它用于单元测试目的。通常用户能够点击它,但我需要一段代码来自动为我点击按钮,以便测试可以验证结果行为是否正确。
  • 也许这会有所帮助stackoverflow.com/questions/2730044/…
  • @Massimo 正如我在问题中提到的,我没有更改getDialogResponse() 的选项,而您链接的问题中的代码要求我这样做。
  • 我会将程序逻辑从 GUI 中分离出来,就像 MVC 一样,并对模型和控件而不是视图进行单元测试。

标签: java swing joptionpane


【解决方案1】:

Sikuli 支持自动查找对话框/按钮并单击它们。也许它会帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多