簡體   English   中英

如何在Java GUI中格式化計時器文本

[英]How to format Timer text in Java GUI

所以這是我的更新代碼,在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用於Java GUI Swing應用程序的新手。

嘗試格式化以所需的格式格式化時間。

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

請查看評論:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM