单击按钮时如何向JLabel添加计时器?

时间:2018-05-16 16:05:38

标签: java user-interface

我正在创建一个简单的秒表程序,其中包含3个按钮(开始,停止和重置)和一个简单的JLabel来显示时间。我在开始按钮中添加了一个动作监听器,因此当单击该按钮时,计时器会启动标签上的时钟。

但是,当我尝试在按钮事件中添加timer变量时,它不允许我这样做。任何人都可以建议我做错了什么或如何改进它?

代码:

public class GuiStopwatch {





    public static void main(String[] args) {
        JFrame frame = new JFrame("Stopwatch");

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
          JPanel panel = new JPanel();

          panel.setLayout(null);

          JButton Startbtn = new JButton("START");  
          JButton Stopbtn = new JButton("STOP");
          JButton Reset = new JButton("RESET");
          JLabel time = new JLabel("Time shows here");
             panel.add(Startbtn);
             panel.add(Stopbtn);
            panel.add(Reset);
            panel.add(time);
             Startbtn.setBounds(50, 150, 100, 35);
             Stopbtn.setBounds(50, 200, 100, 35);
             Reset.setBounds(50, 250, 100, 35);
             time.setBounds(50, 350, 100, 35);
             time.setBackground(Color.black);
             time.setForeground(Color.red);
             frame.add(panel);


             Startbtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Timer timer = new Timer(1,new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            // TODO Auto-generated method stub

                        }
                    })

                }
            });

编辑:

错误是它没有为Timer(int,ActionListener)定义构造函数。

0 个答案:

没有答案
相关问题