有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何通过JButtons关闭JDialog

我正在做一个方法,给用户三个选择,并返回他们单击的一个,现在这个方法有点工作,我单击其中一个选项,如果我单击关闭按钮,它将返回上次单击的选项。我想使对话框在您单击其中一个选项时关闭

public E drawThreeForDecision()
{
    ArrayList<E> c = new ArrayList<E>();
    Component[] options = new Component[3];
    for (int iii = 0; iii < 3; iii++)
    {
        final int loop = iii;
        c.add((E) drawCard());
        JButton button = new JButton(new ImageIcon(((GameEntity) c.get(iii)).getEntityImage()));
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {

            }
        });
        options[iii] = button;
    }
    JOptionPane pane = new JOptionPane("Please select a card", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);
    JDialog dialog = pane.createDialog(null, "DECISION TIME!");
    dialog.setVisible(true);;
    if (pane.getValue() instanceof Integer)
        return (E) pane.getValue();
    return c.get(0);
}

如果有人能在这方面帮助我或提出更好的解决方案,我们将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    使用对话框。处置()actionPerformed()方法中

  2. # 2 楼答案

    试试这些: 对话setModal(真); 对话setVisible(真)