【发布时间】: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 应用程序的新手。
【问题讨论】:
-
欢迎来到 SO。请发帖minimal reproducible example
-
@c0der 你好,我更新了我的帖子..我希望它现在会澄清..我是编程和堆栈溢出的新手..谢谢你的回复...
标签: java swing user-interface time