如何在Java GUI中格式化Timer文本

时间:2017-03-18 07:40:14

标签: java swing user-interface time

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

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 for Java GUI Swing Application。

2 个答案:

答案 0 :(得分:-1)

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

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++;

答案 1 :(得分:-1)

请参阅评论:

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();
    }
}