如何在 Java GUI 中格式化计时器文本



所以这是我的更新代码,我在Java GUI中格式化计时器时遇到问题。

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class deploy extends JFrame {
    private JPanel contentPane;
    Timer tm;
    Timer tm2;
    int i = 0;
    int o = 0;
    public deploy() {
        contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblTimer2 = new JLabel("New label");
        lblTimer2.setForeground(Color.WHITE);
        lblTimer2.setFont(new Font("Tahoma", Font.PLAIN, 20));
        lblTimer2.setBounds(295, 231, 182, 16);
        contentPane.add(lblTimer2);
        tm2 = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                lblTimer2.setText(Integer.toString(o));
                o++;
            }
        });
        JButton btnNewButton = new JButton("Start");
        btnNewButton.setBackground(Color.LIGHT_GRAY);
        btnNewButton.setForeground(Color.BLUE);
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tm2.start();
            }
        });
        btnNewButton.setBounds(289, 257, 89, 32);
        contentPane.add(btnNewButton);
        JButton btnNewButton_1 = new JButton("Stop");
        btnNewButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tm2.stop();
            }
        });
        pack();
        setVisible(true);
    }
    public static void main(String[] args) {
        new deploy();
    }
}

这是我的代码中我需要你帮助的部分。我希望我的 lblTimer2 将显示"00:00"的格式。但是我在这里做的代码被格式化为"0",依此类推。因为我正在创建一个GUI网吧管理软件,我的GUI的功能是给客户计时,在客户完成他/她的工作后,时间将停止,它将计算他/她花费的时间,它将通过计费交易。我是编程新手,并将Eclipse Neon用于Java GUI Swing应用程序。

请参阅评论:

public class deploy extends JFrame {
    private int seconds;
    private SimpleDateFormat df;
    private boolean isRunning;
    private JLabel lblTimer2;
    public deploy() {
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //better avoid null layout managers
        //contentPane.setLayout(null);
        contentPane.setLayout(new BorderLayout());
        lblTimer2 = new JLabel();
        lblTimer2.setForeground(Color.WHITE);
        lblTimer2.setFont(new Font("Tahoma", Font.PLAIN, 20));
        lblTimer2.setPreferredSize(new Dimension(100,30));
        contentPane.add(lblTimer2,BorderLayout.NORTH);
        Timer tm2 = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setTimer();
                seconds++;
            }
        });
        JButton btnNewButton = new JButton("Start");
        btnNewButton.setBackground(Color.LIGHT_GRAY);
        btnNewButton.setForeground(Color.BLUE);
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isRunning) {
                    tm2.stop();
                    btnNewButton.setText("Start");
                }else {
                    tm2.start();
                    btnNewButton.setText("Stop");
                }
                isRunning = !isRunning;
            }
        });
        //btnNewButton.setBounds(289, 257, 89, 32);
        btnNewButton.setPreferredSize(new Dimension(100,30));
        contentPane.add(btnNewButton, BorderLayout.SOUTH);
        //based on SANTOSHKUMAR SINGH answer
        df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        seconds = 0;
        isRunning = false;
        setTimer();
        pack();
        setVisible(true);
    }

    private void setTimer() {
        //based on SANTOSHKUMAR SINGH answer
        Date d = new Date(seconds * 1000L);
        String time = df.format(d);
        lblTimer2.setText(time);
    }
    public static void main(String[] args) {
        new deploy();
    }
}

尝试以所需格式格式化时间。

Date d = new Date(o * 1000L);
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String time = df.format(d);
o++;

最新更新