如果用户在确认对话框中选择no,如何阻止actionListener执行其代码?

时间:2015-05-08 04:40:18

标签: java swing actionlistener

我在按钮上设置了actionListener。如果用户单击它,则会向进度条添加10。我想允许用户取消按下按钮并阻止actionListener中的代码执行。即如果用户在确认对话框中选择“否”,我希望我的程序忽略用户点击。

是尝试/捕获异常的方式吗?

感谢。

btnFightCrime.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
            if (JOptionPane.showConfirmDialog(null, "This action will cost you 5 energy points, and you'll gain 5 action points.", "Proceed?",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                // yes option
            } else if (JOptionPane.YES_NO_OPTION==JOptionPane.YES_OPTION) {

            btnFightCrime.addActionListener(new ActionListener(){   
                @Override
                public void actionPerformed(ActionEvent arg0) {

                    Crimes.randomCrime();
                    fightCrime(iActionBar);
                    Random r = new Random();
                    int delay = 0;
                    //delay is * 1000 because the format for timers is milliseconds
                    timer = new Timer ((delay*0), new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent arg0){
                            btnFightCrime.setVisible(true);
                            btnIgnore.setVisible(true);
                            lblVillainCrime.setVisible(false);
                            lblHeroCrime.setVisible(true);

                            frame.validate();
                            timer.stop();
                        }   
    });
                    timer.start();
                    btnFightCrime.setVisible(false);
                    btnIgnore.setVisible(false);
                    lblHeroCrime.setVisible(false);
                    frame.validate();
                    }
            });
            }
                }
            });


if (JOptionPane.showConfirmDialog(null, "This action will cost you 5 energy points, and you'll gain 5 action points.", "Proceed?",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                btnFightCrime.addActionListener(new ActionListener(){   
                    @Override
                    public void actionPerformed(ActionEvent arg0) {

                        Crimes.randomCrime();
                        fightCrime(iActionBar);
                        Random r = new Random();
                        int delay = 0;
                        //delay is * 1000 because the format for timers is milliseconds
                        timer = new Timer ((delay*0), new ActionListener(){
                            @Override
                            public void actionPerformed(ActionEvent arg0){
                                btnFightCrime.setVisible(true);
                                btnIgnore.setVisible(true);
                                lblVillainCrime.setVisible(false);
                                lblHeroCrime.setVisible(true);

                                frame.validate();
                                timer.stop();
                            }   
        });
                        timer.start();
                        btnFightCrime.setVisible(false);
                        btnIgnore.setVisible(false);
                        lblHeroCrime.setVisible(false);
                        frame.validate();
                        }
                });
            } else if (JOptionPane.YES_NO_OPTION==JOptionPane.NO_OPTION) {


            }

1 个答案:

答案 0 :(得分:1)

只需使用if语句并检查确认对话框的返回状态......

int result = JOptionPane.showConfirmDialog(this, "Are you sure you want to destory the world?", "Destory World", JOptionPane.YES_OPTION);
if (result == JOptionPane.YES_OPTION) {
    // Blow it up
}

当然,你可以使用相同的技术,但是使用flag,所以如果确认过程与更新过程分开,那么它就会变得更加困难

基于更新后的代码

我要在黑暗中刺伤,猜测"这就是你真正的意思......

btnFightCrime.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (JOptionPane.showConfirmDialog(null, "This action will cost you 5 energy points, and you'll gain 5 action points.", "Proceed?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            Crimes.randomCrime();
            fightCrime(iActionBar);
            Random r = new Random();
            int delay = 0;
            //delay is * 1000 because the format for timers is milliseconds
            timer = new Timer((delay * 0), new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    btnFightCrime.setVisible(true);
                    btnIgnore.setVisible(true);
                    lblVillainCrime.setVisible(false);
                    lblHeroCrime.setVisible(true);

                    frame.validate();
                    timer.stop();
                }
            });
            timer.start();
            btnFightCrime.setVisible(false);
            btnIgnore.setVisible(false);
            lblHeroCrime.setVisible(false);
            frame.validate();

        }
    }
};

执行} else if (JOptionPane.YES_NO_OPTION==JOptionPane.YES_OPTION) {没有意义,这将(很可能)永远是false(或者至少是完全不可靠)。

ActionListener内为同一个按钮添加另一个ActionListener并没有任何意义。只需询问用户,"你想做到这一点吗?",如果答案是"是",做,否则,他们已经点击了按钮......