【问题标题】:How to format Timer text in Java GUI如何在 Java GUI 中格式化计时器文本
【发布时间】:2017-08-09 20:29:22
【问题描述】:

所以这是我的更新代码,我在 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 应用程序的新手。

【问题讨论】:

标签: java swing user-interface time


【解决方案1】:

请看cmets:

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

【讨论】:

  • 天哪,它有效!太感谢了! @c0der 但是在我的设计选项卡中发生了一些事情,我可以在其中拖放框架的组件。它试图调整大小,但在我运行之前它不起作用,它起作用但在我停止运行后我无法编辑我的其他标签和按钮,甚至无法移动它。 JPanel 发生了什么?
  • 你好@c0der 我在重置计时器时遇到问题。我需要你的帮助..我创建了一个按钮,我可以在其中重置计时器并使用 tm2.restart();但它不工作......
【解决方案2】:

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

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

【讨论】:

  • 这对你有帮助,现在检查
  • 谢谢 SANTOSHKUMARSINGH 它有效!感谢@c0der 正确放置您提供的代码..
  • 我已经接受你的回答。我是 Stack Overflow 的新手。我开始熟悉这个网站。无论如何,非常感谢你
  • 我看到您“不接受”这个问题并接受了我的问题。 @SANTOSHKUMARSINGH 是正确且有帮助的。请注意,您也可以对答案投票。
  • 你好@c0der 我还没有特权对他的答案进行投票。虽然它很有帮助。但对我来说,最好的解决方案是你的,所以我接受了。如果我有权对答案投票,我也会对此投票。
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多