0

我有 JButtons“暂停”和“取消暂停”。当用户暂停程序时,应禁用暂停按钮并启用取消暂停按钮。我不知道怎么写。取消暂停按钮有效,但暂停按钮无效,因为“无法解决取消暂停”。如何处理?这是我的代码:

final JButton pause = new JButton("Pause");

    pause.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            try {
                Url.pauseThread();
                pause.setEnabled(false); //this works
                unpause.setEnabled(true); //this does NOT work - "not resolved"

            } catch (InterruptedException e1) {

                e1.printStackTrace();
            }  

        }
    });

    final JButton unpause = new JButton("Unpause");

    unpause.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Url.resumeThread();
                pause.setEnabled(true); // this works
                unpause.setEnabled(false); // this works
            } catch (InterruptedException e1) {

                e1.printStackTrace();
            }
        }
    });
4

1 回答 1

2

声明暂停和取消暂停按钮,然后添加侦听器。

final JButton pause = new JButton("Pause");
final JButton unpause = new JButton("Unpause");
    pause.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            try {
                Url.pauseThread();
                pause.setEnabled(false); //this works
                unpause.setEnabled(true); //this does NOT work - "not resolved"

            } catch (InterruptedException e1) {

                e1.printStackTrace();
            }  

        }
    });



    unpause.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Url.resumeThread();
                pause.setEnabled(true); // this works
                unpause.setEnabled(false); // this works
            } catch (InterruptedException e1) {

                e1.printStackTrace();
            }
        }
    });

基本上,您setEnabled甚至在声明取消暂停按钮之前从暂停按钮侦听器中的取消暂停按钮调用。

于 2013-08-21T22:23:33.063 回答