XIV thread design

First, the code

 package shiyan14;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShiYan14 extends JFrame{/*** */
private static final serialVersionUID = 1L long; // definition component
private JLabel lblTime;
private JTextField txtInput;
private JButton btnEnter; // Constructors
public ShiYan14 () {// set of related attributes form
super("ShiYan14");
this.setSize(300,200);
this.setLayout(null);
this.setLocation (100,50); // Create the component
this.lblTime = new JLabel ( "Please enter the countdown time");
this.lblTime.setBounds(30,20,200,30);
this.txtInput = new JTextField();
this.txtInput.setBounds(30,70,100,30);
this.btnEnter = new JButton("确定");
this.btnEnter.setBounds (150,70,70,30); // register listener to JTextField
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {}
public void keyReleased(KeyEvent ke) {}
public void keyTyped(KeyEvent ke) {//
tInput_KeyTyped (to);
}
}); // register listener to the JButton
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed (ae);}}); // Add the component on the form
add(lblTime);
add(txtInput);
add (btnEnter); // display the form
this.setVisible (true);} // Event processing is input, the user can control the input digital
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() < '0' || ke.getKeyChar() > '9'){
ke.setKeyChar('\0');
}
} // event handler when the click of a button
public void btnEnter_ActionPerformed (ActionEvent ae) {// get user input countdown time String strTime = this.txtInput.getText ();
if (strTime.equals ( "")) {// detect whether the user input
this.lblTime.setText ( "You have not entered, enter!");}
else {Integer time = Integer.parseInt (strTime); // create a thread
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start (); // create
Hours hours hours hours = new ();
timer.schedule (new TimerTask () {// start other programs
public void run() {System.out.print("ok");
}
}, time * 1000);
}
} // startup form
public static void main(String[] args){
new ShiYan14();
}
} // thread class time
class TimeThread extends Thread{
private JLabel lblTime;
private int time; // constructor passed in time JLabel display and countdown events.
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;this.time = time;
} // run method
public void run(){
while (time> 0) {// display the time remaining
this.lblTime.setText ( "time left:" + time + "s"); // remaining time is reduced
time--;
try {Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace ();
}
}
}
}

Second, the operating results

 

 

 

 

 

 

 

Third, feelings and experiences

JAVA application methods will run from main (), which runs in a thread, the main thread is called, an error in the preparation of the main function. Complete the code in the students help.

Guess you like

Origin www.cnblogs.com/zy2023/p/11110860.html