8

好的,所以我做了一个简单的程序,每次单击按钮时都会将值添加到计数器。现在,我想添加“自动”按钮功能,以在单击“自动”按钮时增加计数器的值。我遇到了问题,因为它不会在屏幕上呈现每个计数器值,而是在循环完成时更新值。这是我的代码:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;


public class Gui extends JFrame{

    private static final long serialVersionUID = 1L;

    private JButton uselesButton;

    private JButton autoButton;

    private FlowLayout layout;
    private long counter = 0;

    public Gui() {
        super("Button");
        layout = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(layout);

        uselesButton = new JButton(String.format("Pressed %d times", counter));
        add(uselesButton);
        uselesButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                counter++;
                uselesButton.setText(String.format("Pressed %d times", counter));
            }

        });

        autoButton = new JButton("Auto");
        add(autoButton);
        autoButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                        for(long i =0; i < 99999999;i++) {
                        try {
                            TimeUnit.MILLISECONDS.sleep(10);
                        } catch (InterruptedException e1) {
                            System.out.println("ERROR");
                        }
                        counter = i;
                        uselesButton.setText(String.format("Pressed %d times", counter));
                    }
                    }
        });
    }
}

请记住,我是初学者...所有帮助表示赞赏:)

4

3 回答 3

6

看看关于如何使用Swing Timer的教程,然后看看我的解决方案:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;
    private JButton uselesButton;
    private JButton autoButton;
    private FlowLayout layout;
    private long counter = 0;
    private javax.swing.Timer timer;

    public Gui() {
        super("Button");
        layout = new FlowLayout(FlowLayout.CENTER);
        setLayout(layout);
        setDefaultCloseOperation(3);
        setSize(300, 300);
        setLocationRelativeTo(null);

        //initialing swing timer
        timer = new javax.swing.Timer(100, getButtonAction());

        autoButton = new JButton("Auto");
        add(autoButton);
        autoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!timer.isRunning()) {
                    timer.start();
                } else {
                    timer.stop();
                }
            }
        });
    }

    private ActionListener getButtonAction() {
        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                autoButton.setText(String.format("Pressed %d times", ++counter));
                if (counter > 1000) {
                    timer.stop();
                }
            }
        };
        return action;
    }

    public static void main(String... args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
}
于 2013-07-07T11:59:36.270 回答
1

当您进入此循环时,您的代码会阻塞 GUI 线程 (EDT)(GUI 将挂起,按钮在您完成之前不会更新),因此您应该将代码添加到另一个工作线程中:

autoButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for(long i =0; i < 99999999;i++) {
                                try {
                                    TimeUnit.MILLISECONDS.sleep(10);
                                } catch (InterruptedException e1) {
                                    System.out.println("ERROR");
                                }
                                counter = i;

                                java.awt.EventQueue.invokeLater(new Runnable() {
                                      public void run() {
                                         uselesButton.setText(String.format("Pressed %d times", counter));
                                      }
                                });
                            }
                        }
                    }).start();
            }
        });
于 2013-07-07T11:46:39.997 回答
0

这里的问题是系统处于循环中,因此无法绘制更改。为此,您需要打开一个新线程。新线程将执行循环,主线程将重新绘制表单。

还有一件事,你不应该在主线程上睡觉。您可以使用每 10 毫秒计时一次的计时器,而不是sleep(10) 这里的示例

于 2013-07-07T11:40:55.647 回答