📄 stopwatch.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StopWatch extends JFrame
{
JPanel mainPane,disPane,ctrPane,lPane;
JButton startbutton,pausebutton,stopbutton,rebutton;
JLabel[] dislabel;
JLabel[] clabel;
JLabel plabel;
Timer[] t;
int counts=0;
int size=100;
int MaxSize;
int ms=0,s=0,m=0,h=0;
String input;
boolean ch=false;
public StopWatch()
{
mainPane=new JPanel();
setContentPane(mainPane);
mainPane.setLayout(new BoxLayout(mainPane,BoxLayout.Y_AXIS));
disPane=new JPanel();
mainPane.add(disPane);
disPane.setSize(250,100);
disPane.setLayout(new GridLayout(1,7,2,2));
ctrPane=new JPanel();
mainPane.add(ctrPane);
ctrPane.setSize(250,100);
ctrPane.setLayout(new GridLayout(1,4,10,10));
input=JOptionPane.showInputDialog(this,"Please input the number");
if(input==null)
{
System.exit(0);
}
MaxSize=Integer.parseInt(input);
dislabel=new JLabel[4];
for(int i=0;i<4;i++)
{
dislabel[i]=new JLabel("00");
}
clabel=new JLabel[2];
for(int i=0;i<2;i++)
{
clabel[i]=new JLabel(":");
}
plabel=new JLabel(".");
disPane.add(dislabel[3]);
disPane.add(clabel[1]);
disPane.add(dislabel[2]);
disPane.add(clabel[0]);
disPane.add(dislabel[1]);
disPane.add(plabel);
disPane.add(dislabel[0]);
lPane=new JPanel();
mainPane.add(lPane);
lPane.setLayout(new BoxLayout(lPane,BoxLayout.Y_AXIS));
startbutton=new JButton("Start");
startbutton.setSize(50,30);
startbutton.addActionListener(new Start());
pausebutton=new JButton("Pause");
pausebutton.setSize(50,30);
pausebutton.addActionListener(new Pause());
stopbutton=new JButton("Stop");
stopbutton.setSize(50,30);
stopbutton.addActionListener(new Stop());
rebutton=new JButton("Reset");
rebutton.setSize(50,30);
rebutton.addActionListener(new Reset());
ctrPane.add(startbutton);
ctrPane.add(pausebutton);
ctrPane.add(stopbutton);
ctrPane.add(rebutton);
t=new Timer[4];
t[0]=new Timer(10,new TimerListener());
t[1]=new Timer(1000,new TimerListener());
t[2]=new Timer(60000,new TimerListener());
t[3]=new Timer(3600000,new TimerListener());
setTitle("StopWatch");
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200,200);
setSize(350,80);
//pack();
validate();
}
class Start implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t[0].start();
ch=true;
if(counts==MaxSize)
{
lPane.removeAll();
setSize(350,80);
size=100;
counts=0;
}
validate();
}
}
class Pause implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<4;i++)
{
t[i].stop();
}
}
}
class Stop implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(ch==true)
{
counts++;
lp p=new lp();
size=size+35;
setSize(350,size);
//lPane.setSize(250,400);
p.ldis.setText("No."+counts+" : "+h+":"+m+":"+s+"."+ms);
lPane.add(p);
validate();
if(counts==MaxSize)
{
dislabel[0].setText("00");
dislabel[1].setText("00");
dislabel[2].setText("00");
dislabel[3].setText("00");
t[0].stop();
t[1].stop();
t[2].stop();
t[3].stop();
counts=MaxSize;
ch=false;
ms=0;s=0;m=0;h=0;
}
}
}
}
class Reset implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
new StopWatch();
}
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ms++;
dislabel[0].setText(""+ms);
if(ms==99)
{
t[1].start();
s++;
dislabel[1].setText(""+s);
ms=0;
if(s==59)
{
t[2].start();
m++;
dislabel[2].setText(""+m);
s=0;
if(m==59)
{
t[3].start();
h++;
dislabel[4].setText(""+h);
m=0;
}
}
}
}
}
public static void main(String[] args)
{
new StopWatch();
}
}
class lp extends JPanel
{
JTextField ldis;
lp()
{
ldis=new JTextField();
ldis.setEditable(false);
this.setSize(250,20);
this.add(ldis);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -