import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;public class MyTime{
public static void main(String args[])
{
launchFrame frame=new launchFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class launchFrame extends JFrame implements ActionListener {
JButton btStart,btStop,btSuspend;
JTextField textField;
Timer time;
int timeSecond,timeMinute,timeHour;
JPanel panel;
public launchFrame()
{
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(640,360);
this.setLocation((screenSize.width-640)/2,(screenSize.height-360)/2);
this.setVisible(true);
this.setResizable(false);
time=new Timer(1000,this);
panel=new JPanel();
btStart=new JButton("开始");
btStop=new JButton("停止");
btSuspend=new JButton("暂停");
textField=new JTextField(20);
btStart.addActionListener(this);
btStop.addActionListener(this);
btSuspend.addActionListener(this);
textField.addActionListener(this);
btStop.setEnabled(false);
btSuspend.setEnabled(false);
panel.add(btStart);
panel.add(btSuspend);
panel.add(btStop);
panel.add(textField);
Container contentPane=getContentPane();
contentPane.add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==time)
{
if(timeSecond==60)
{
timeMinute++;
timeSecond=0;
}
else if(timeMinute==60)
{
timeHour++;
timeMinute=0;
}
timeSecond++;
String strTime=String.valueOf(timeHour)+"时"+String.valueOf(timeMinute)+"分"+String.valueOf(timeSecond)+"秒";
textField.setText(strTime);
}
else if(e.getSource()==btStart)
{
time.start();
btStart.setEnabled(false);
btSuspend.setEnabled(true);
btStop.setEnabled(true);
}
else if(e.getSource()==btSuspend)
{
time.stop();
btStart.setEnabled(true);
btSuspend.setEnabled(false);
btStop.setEnabled(true);
}
else if(e.getSource()==btStop)
{
time.stop();
timeSecond=0;
timeMinute=0;
timeHour=0;
btStart.setEnabled(true);
btSuspend.setEnabled(false);
btStop.setEnabled(false);
}
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;public class MyTime{
public static void main(String args[])
{
launchFrame frame=new launchFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class launchFrame extends JFrame implements ActionListener {
JButton btStart,btStop,btSuspend;
JTextField textField;
Timer time;
int timeSecond,timeMinute,timeHour;
JPanel panel;
public launchFrame()
{
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(640,360);
this.setLocation((screenSize.width-640)/2,(screenSize.height-360)/2);
this.setVisible(true);
this.setResizable(false);
time=new Timer(1000,this);
panel=new JPanel();
btStart=new JButton("开始");
btStop=new JButton("停止");
btSuspend=new JButton("暂停");
textField=new JTextField(20);
btStart.addActionListener(this);
btStop.addActionListener(this);
btSuspend.addActionListener(this);
textField.addActionListener(this);
btStop.setEnabled(false);
btSuspend.setEnabled(false);
panel.add(btStart);
panel.add(btSuspend);
panel.add(btStop);
panel.add(textField);
Container contentPane=getContentPane();
contentPane.add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==time)
{
if(timeSecond==60)
{
timeMinute++;
timeSecond=0;
}
else if(timeMinute==60)
{
timeHour++;
timeMinute=0;
}
timeSecond++;
String strTime=String.valueOf(timeHour)+"时"+String.valueOf(timeMinute)+"分"+String.valueOf(timeSecond)+"秒";
textField.setText(strTime);
}
else if(e.getSource()==btStart)
{
time.start();
btStart.setEnabled(false);
btSuspend.setEnabled(true);
btStop.setEnabled(true);
}
else if(e.getSource()==btSuspend)
{
time.stop();
btStart.setEnabled(true);
btSuspend.setEnabled(false);
btStop.setEnabled(true);
}
else if(e.getSource()==btStop)
{
time.stop();
timeSecond=0;
timeMinute=0;
timeHour=0;
btStart.setEnabled(true);
btSuspend.setEnabled(false);
btStop.setEnabled(false);
}
}
}
time.stop();
timeSecond = 0;
timeMinute = 0;
timeHour = 0;
textField.setText("0时0分0秒"); //把这句话加进去就归零了
btStart.setEnabled(true);
btSuspend.setEnabled(false);
btStop.setEnabled(false);
}
当你点下“停止”按钮,你把Timer也停了,Timer不再触发新的事件,因此,界面不会更新