package swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class BANG{
public static volatile int T2;
public static int time;
public JFrame jFrame;
public static int i,j,x,y;
public Button button=new Button("begin");
public Button button2=new Button("stop");
public static TextField textField=new TextField("60",8);
public BANG(String name){
i=0;
j=0;
x=50;
y=120;
jFrame=new JFrame();
jFrame.setName(name);
jFrame.setTitle("BANG!!!");
jFrame.setSize(x,y);
jFrame.setLayout(new FlowLayout());
jFrame.add(new Label("time is:"));
jFrame.add(textField);
jFrame.add(new Label("unit:s"));
jFrame.add(button);
jFrame.add(button2);
jFrame.setVisible(true);
}
public void run(){
{
Thread1 T=new Thread1();
T.start();
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Thread1.stop=true;
}
});
}
}
public static void main(String[] args) {
BANG bang=new BANG("BANG!");
bang.jFrame.addWindowListener(new WindowListener(){
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
Thread1.stop=true;
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
bang.button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String tim=bang.textField.getText();
try {
time = Integer.parseInt(tim);
}catch (Exception e1){
System.out.println("异常出来");
time=T2;
}
finally {
Thread1.stop=false;
System.out.println(time);
bang.run();
}
}
});
}
}
package swing;
import javax.swing.*;
import java.awt.*;
public class Thread1 extends Thread{
public static volatile boolean stop = false;
public void run(){
while(BANG.time>0 && !stop){
System.out.println("时间:"+BANG.time);
BANG.time--;
BANG.textField.setText(BANG.time+"s");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(BANG.time==0 && stop==false){
begin();
}
else {
BANG.T2=BANG.time;
BANG.textField.setText("stop");
System.out.println("退出");
}
}
public void begin(){
while(true){
JFrame jFrame1=new JFrame("dead Dead!!!!");
jFrame1.setSize(50,60);
BANG.i=BANG.i+10;
BANG.j=BANG.j+10;
jFrame1.setLocation(BANG.i,BANG.j);
jFrame1.add(new Label("BANG BANG!!"));
jFrame1.setVisible(true);
}
}
}