如何使用showMessageDialog中的选项来做事情?

时间:2014-06-03 04:03:48

标签: java jbutton joptionpane

我在Java上制作游戏,我正在做一个角色选择菜单。在该菜单中,我有字符,如果用户点击某个字符,则会出现JOptionPane.showMessageDialog,显示该字符的统计信息。所以我的问题是,如果用户点击"确定"这是在使用函数时自动创建的,如何选择角色?

JButton chuck = new JButton(new ImageIcon("8bitChuckNorris.jpg"));//this part of program runs this if user picks lizard
chuck.setSize(210,175); //sets size of button
chuck.setLocation(300,317); //sets location of button
chuck.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e) {    
        JOptionPane.showMessageDialog(null, "\t\tSTATS\nAttack\ndefence\nspecial");
    }
});

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是提供ActionListener之后可以调用的方法 选项窗格已关闭,例如......

chuck.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent e) {    
        JOptionPane.showMessageDialog(null, "\t\t                  STATS\nAttack\ndefence\nspecial");
        characterSelected("chuck"); // Pass whatever you need to identify the character
    }
});

<强>更新

使用JOptionPane.showConfirmDialog会更容易,因为它实际上会返回一个int结果,表示用户选择的选项(如果他们在没有选择任何内容的情况下解除对话框,则为-1)

 switch (JOptionPane.showConfirmDialog(null, "\t\t                  STATS\nAttack\ndefence\nspecial", "Character", JOptionPane.OK_CANCEL_OPTION)) {
     case JOptionPane.OK:
         // Process selection...
         break;
 }
相关问题