如何在Java中自动点击JOptionPane中的'YES'?

How to automatically click 'YES' in JOptionPane in Java?

我正在使用 Cucumber 来测试开源 Java Swing 应用程序。我尝试了很多方法在弹出的 JOptionPane 上单击 'Yes'(比如设置 ch = 0 和 ch = JOptionPane.YES_OPTION),但它不起作用。 这是代码:

public JButton btnAdd;
public int ch;

btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name=textField.getText();
                double bal=Double.parseDouble(textField_1.getText());
                double maxw=Double.parseDouble(textField_2.getText());
                if(bal<2000) {
                    JOptionPane.showMessageDialog(getComponent(0), "Minimum Limit 5000", "Warning", 0);
                    textField.setText(null);
                    textField_1.setText(null);
                    textField_2.setText(null);
                }
                else {
                    if(name==null||bal<=0||maxw<=0) {
                        JOptionPane.showMessageDialog(getComponent(0),"Typing Mismatch!! Try Again");
                        textField.setText(null);
                        textField_1.setText(null);
                        textField_2.setText(null);
                    }
                    else {
                        ch=JOptionPane.showConfirmDialog(getComponent(0), "Confirm?");
                        if(ch==0) {
                            int index = FileIO.bank.addAccount(name, bal, maxw);
                            DisplayList.arr.addElement(FileIO.bank.getAccounts()[index].toString());
                            JOptionPane.showMessageDialog(getComponent(0),"Added Successfully");
                            dispose();
                        }
                        else {
                            JOptionPane.showMessageDialog(getComponent(0),"Failed");
                            textField.setText(null);
                            textField_1.setText(null);
                            textField_2.setText(null);
                        }
                        textField.setText(null);
                        textField_1.setText(null);
                        textField_2.setText(null);
                    }
                }
            }

有什么方法可以点击 'Yes' 吗?谢谢!

这是一个例子,如何在出现的所有选项对话框中按“是”按钮

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class PressOptionButton {

    private final AWTEventListener windowListener = this::pressYes;
    
    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH); // make names of buttons in JOptionPane English
        SwingUtilities.invokeLater(new PressOptionButton()::initUI);
    }
    
    private void initUI() {
        Toolkit.getDefaultToolkit().addAWTEventListener(windowListener, AWTEvent.WINDOW_EVENT_MASK);
        JOptionPane.showConfirmDialog(null, "Here is a simple confirm dialog", "Confirm!", JOptionPane.YES_NO_CANCEL_OPTION);
        JOptionPane.showConfirmDialog(null, "Here is another confirm dialog", "Confirm!", JOptionPane.YES_NO_OPTION);
        JPanel p = new JPanel(new BorderLayout(10, 10));
        p.add(new JLabel("Enter something"), BorderLayout.WEST);
        p.add(new JTextField(10));
        JOptionPane.showConfirmDialog(null, p, "Confirm!", JOptionPane.YES_NO_CANCEL_OPTION);
        // don't forget to remove the listener when no longer required.
        Toolkit.getDefaultToolkit().removeAWTEventListener(windowListener);
    }
    
    private void pressYes(AWTEvent e) {
        if (e instanceof WindowEvent && ((WindowEvent) e).getID() == WindowEvent.WINDOW_ACTIVATED) {
            Window w = ((WindowEvent) e).getWindow();
            List<JOptionPane> pane = getAllChildrenOfClass(w, JOptionPane.class);
            if (pane.size() == 1) {
                JOptionPane root = pane.get(0);
                for (JButton b : getAllChildrenOfClass(root, JButton.class)) {
                    if ("Yes".equals(b.getText())) {
                        b.doClick(2000); // if you want no delay, use 0 as parameter
                        break;
                    }
                }
            }
        }
    }
    
    /**
     * Searches for all children of the given component which are instances of the given class.
     *
     * @param aRoot start object for search. May not be null.
     * @param aClass class to search. May not be null.
     * @param <E> class of component.
     * @return list of all children of the given component which are instances of the given class. Never null.
     */
    public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass) {
        return getAllChildrenOfClass(aRoot, aClass, e -> true);
    }

    /**
     * Searches for all children of the given component which are instances of the given class and satisfies the given condition.
     *
     * @param aRoot start object for search. May not be null.
     * @param aClass class to search. May not be null.
     * @param condition condition to be satisfied. May not be null.
     * @param <E> class of component.
     * @return list of all children of the given component which are instances of the given class. Never null.
     */
    public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass, Predicate<E> condition) {
        final List<E> result = new ArrayList<>();
        final Component[] children = aRoot.getComponents();
        for (final Component c : children) {
            if (aClass.isInstance(c) && condition.test(aClass.cast(c))) {
                result.add(aClass.cast(c));
            }
            if (c instanceof Container) {
                result.addAll(getAllChildrenOfClass((Container) c, aClass, condition));
            }
        }
        return result;
    }
}