单击按钮后,Java setText不会更新

时间:2018-01-31 09:56:40

标签: java swing

我正在使用按钮来启动和停止计时器。

我需要帮助解决这个问题......当我按下"开始"按钮有效(使用System.out.print进行测试)但setText没有为我更新。

但是,如果将t.start();放在frame.setVisible(true);之后而不是start = 1;之后,setText确实更新了...但是没有我按下&#34 ;开始"按钮。如何在"开始"之后使setText更新?按下按钮?

public class Cook implements ActionListener{

    private enum Actions {
        Start, Stop, Setting, Exit
      }

    JLabel minsleft;
    int start = 0, counter, stop = 0, close = 0, whileloop = 0;

     JFrame frame = new JFrame("SMART STOVE");
    public void Cook() {

        Cook instance = new Cook();
        // TODO Auto-generated method stub
         JLabel minsleft = new JLabel();
         minsleft.setFont(new Font("Serif", Font.BOLD, 17));
         minsleft.setBounds(100,300,250,40);

         try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src\\cook.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

            JButton Start = new JButton("Start");
            Start.setFont(new Font("monospaced", Font.BOLD, 30));
            Start.setActionCommand(Actions.Start.name());
            Start.addActionListener(instance);          
            Start.setBounds(150, 180, 200, 100);
            frame.add(Start);

            JButton Stop = new JButton("Stop");
            Stop.setFont(new Font("monospaced", Font.BOLD, 30));
            Stop.setActionCommand(Actions.Stop.name());
            Stop.addActionListener(instance);           
            Stop.setBounds(360, 180, 200, 100);

            frame.add(Stop);
            countdownLabel.setBounds(360, 250, 200, 100);
            frame.add(countdownLabel);

            frame.setLayout(null);
            frame.setSize(700, 500);
            frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        if (evt.getActionCommand() == Actions.Start.name()) {
            start = 1;
            t.start();
          }

        if (evt.getActionCommand() == Actions.Stop.name()) {
            close = 1;
          }
        System.out.println("Done");
    }

    final JLabel countdownLabel = new JLabel("Waiting. . . ");    
    public Timer t = new Timer(1000, new ActionListener() {
        int time = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            time++;
            countdownLabel.setText(format(time / 60) + ":" + format(time % 60));
            System.out.println(format(time / 60) + ":" + format(time % 60));
            if (close == 1) {
                final Timer timer = (Timer) e.getSource();
                timer.stop();
                System.out.println("Stopped");
            }
        }
    });

    private static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

}

1 个答案:

答案 0 :(得分:0)

我想知道你是如何运行代码的,而没有main。所以我添加了一个

public static void main(String[] args) {
    new Cook().Cook();
}

然后我想知道你为什么要在Cook方法中创建新的Cook实例(非常糟糕的Java命名)。

Cook instance = new Cook();

所以我删除了它并将Start.addActionListener(instance);更改为Start.addActionListener(this);,现在它正在运行......

我还会添加默认关闭操作,否则它不会真正关闭...

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

完整代码:

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class Cook implements ActionListener{

    private enum Actions {
        Start, Stop, Setting, Exit
      }

    JLabel minsleft;
    int start = 0, counter, stop = 0, close = 0, whileloop = 0;

     JFrame frame = new JFrame("SMART STOVE");
    public void Cook() {

//        Cook instance = new Cook();
        // TODO Auto-generated method stub
         JLabel minsleft = new JLabel();
         minsleft.setFont(new Font("Serif", Font.BOLD, 17));
         minsleft.setBounds(100,300,250,40);

         try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src\\cook.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

            JButton Start = new JButton("Start");
            Start.setFont(new Font("monospaced", Font.BOLD, 30));
            Start.setActionCommand(Actions.Start.name());
            Start.addActionListener(this);          
            Start.setBounds(150, 180, 200, 100);
            frame.add(Start);

            JButton Stop = new JButton("Stop");
            Stop.setFont(new Font("monospaced", Font.BOLD, 30));
            Stop.setActionCommand(Actions.Stop.name());
            Stop.addActionListener(this);           
            Stop.setBounds(360, 180, 200, 100);

            frame.add(Stop);
            countdownLabel.setBounds(360, 250, 200, 100);
            frame.add(countdownLabel);

            frame.setLayout(null);
            frame.setSize(700, 500);
            frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        if (evt.getActionCommand() == Actions.Start.name()) {
            start = 1;
            t.start();
          }

        if (evt.getActionCommand() == Actions.Stop.name()) {
            close = 1;
          }
        System.out.println("Done");
    }

    final JLabel countdownLabel = new JLabel("Waiting. . . ");    
    public Timer t = new Timer(1000, new ActionListener() {
        int time = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            time++;
            countdownLabel.setText(format(time / 60) + ":" + format(time % 60));
            System.out.println(format(time / 60) + ":" + format(time % 60));
            if (close == 1) {
                final Timer timer = (Timer) e.getSource();
                timer.stop();
                System.out.println("Stopped");
            }
        }
    });

    private static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

    public static void main(String[] args) {
        new Cook().Cook();
    }

}