Вы находитесь на странице: 1из 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class Watch extends JFrame implements ActionListener
{
private static JTextArea area;
private JButton btn[];
private JPanel pane;
private Container con;
private SWThread stp;
static int min=0,sec=0;
static String str="";
private String btnStr[]={"Start","Stop","Reset","Close"};
boolean state=false,blocked=false;
public Watch()
{
super("Stop Watch Experiment");
con=getContentPane();
pane=new JPanel();
btn=new JButton[4];
area=new JTextArea(5,30);
stp=new SWThread();
area.setBackground(Color.GREEN);
area.setFont(new Font("Lucida Writing",Font.BOLD,200));
area.setForeground(Color.BLUE);
pane.setLayout(new GridLayout(2,2));
for(int i=0;i<btn.length;i++)
{
btn[i]=new JButton(btnStr[i]);
btn[i].addActionListener(this);
pane.add(btn[i]);
}
con.add(area,BorderLayout.CENTER);
con.add(pane,BorderLayout.SOUTH);
setSize(530,400);
setVisible(true);
}
public static void upDateWatch()
{
DecimalFormat precision=new DecimalFormat("00");
sec++;
if(sec==60)
{
min++;
sec=0;
}
str=precision.format(min);
str+=":";
str+=precision.format(sec);
area.setText(str);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn[0])
{
if(!blocked)
{
stp.setDaemon(true);
stp.start();
stp.setRunning();
}
else
{
stp.resumeThread();
}
}
else if(e.getSource()==btn[1])
{
try
{
blocked=true;
new Thread()
{
public void run()
{
stp.stopThread();
}
}.start();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
else if(e.getSource()==btn[2])
{
min=sec=0;
}
else
{
System.exit(0);
}
}
public static void main(String args[])
{
Watch a=new Watch();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Вам также может понравиться