我正在制作一个方法,为用户提供三个选择并返回他们点击的那个,现在方法有点工作我单击其中一个选项,如果我单击关闭按钮它返回最后一次单击。我想这样做,以便在您单击其中一个选项
时关闭对话框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);
}
如果有人可以帮我这个或提出更好的解决方案,我们将不胜感激!
答案 0 :(得分:0)
试试这些: dialog.setModal(真); dialog.setVisible(真);
答案 1 :(得分:0)
在 actionPerformed()方法中使用 dialog.dispose(); 。