我想创建一个具有超时/超时功能的系统。
在将该功能包含到我的系统之前,我尝试了此代码作为试用版:
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Time extends JFrame implements ActionListener {
Date now = new Date();
private JLabel time;
private JButton getTime;
private SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss");
public Time()
{
setLayout(null);
setSize(500,300);
JLabel time = new JLabel("00:00:00");
time.setSize(100,100);
time.setLocation(40,40);
JButton getTime = new JButton("GET TIME");
getTime.addActionListener(this);
getTime.setSize(90,30);
getTime.setLocation(90,70);
Container pane = getContentPane();
pane.add(time);
pane.add(getTime);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "GET TIME")
{
JOptionPane.showMessageDialog(null, "Time "+dateFormatter.format(now),
"Time.",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
new Time();
}
}
它获取当前时间,但是当我再次单击按钮时它仍然给出相同的时间。只有当我关闭 UI 时它才会改变。