Use multithreading technology to make your Swing respond to various events in a timely manner

Source: Internet
Author: User
Using multithreading technology, Swing can respond to various events in a timely manner-general Linux technology-Linux programming and kernel information. For more information, see the following. 1. Example of using a thread
Package untitled1;

Import javax. swing .*;
Import java. awt. event .*;
Import java. awt .*;
Import com. borland. jbcl. layout .*;


/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @ Author
* @ Version 1.0
*/

Public class TestThread extends JFrame {
JPanel jPanel1 = new JPanel ();
XYLayout xYLayout1 = new XYLayout ();
JButton startButton = new JButton ();
JButton stopButton = new JButton ();
MyThread thread = null;
Public TestThread (){
Try {
JbInit ();

}
Catch (Exception e ){
E. printStackTrace ();
}
}
Private void jbInit () throws Exception {
JPanel1.setLayout (xYLayout1 );
StartButton. setText ("start ");
StartButton. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Startbutton_actionreceivmed (e );
}
});
StopButton. setText ("stop ");
StopButton. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Stopbutton_actionreceivmed (e );
}
});
This. getContentPane (). add (jPanel1, BorderLayout. CENTER );
JPanel1.add (startButton, new XYConstraints (36,105, 82, 30 ));
JPanel1.add (stopButton, new XYConstraints (160,108,100, 31 ));
}

Void startbutton_actionreceivmed (ActionEvent e ){
If (thread! = Null) thread. stop ();
Thread = new MyThread ();
Thread. start ();
}

Void stopbutton_actionreceivmed (ActionEvent e ){
If (thread! = Null) thread. stop ();
Thread = null;
}
Public static void main (String [] args)
{
TestThread test = new TestThread ();
Test. setSize (300,400 );
Test. show ();
}

Private class MyThread extends Thread
{
Public MyThread (){
}
Public void run (){
While (true ){
Try {
Sleep (100 );
} Catch (InterruptedException e ){}
System. out. println ("this is a test! ");
}
}
}
}




2. Example of not using a thread
Package untitled1;
Import javax. swing .*;
Import java. awt. event .*;
Import java. awt .*;
Import com. borland. jbcl. layout .*;
Public class NoThread extends JFrame
{
JPanel jPanel1 = new JPanel ();
XYLayout xYLayout1 = new XYLayout ();
JButton startButton = new JButton ();
JButton stopButton = new JButton ();
Private boolean flagTrue = true;

Public static void main (String [] args)
{
NoThread test = new NoThread ();
Test. setSize (300,400 );
Test. show ();
}

Public NoThread (){
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
Private void jbInit () throws Exception {
JPanel1.setLayout (xYLayout1 );
StartButton. setText ("start ");
StartButton. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Startbutton_actionreceivmed (e );
}
});
StopButton. setText ("stop ");
StopButton. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Stopbutton_actionreceivmed (e );
}
});
This. getContentPane (). add (jPanel1, BorderLayout. CENTER );
JPanel1.add (startButton, new XYConstraints (27,149,-1,-1 ));
JPanel1.add (stopButton, new XYConstraints (182,151,-1,-1 ));
}

Void startbutton_actionreceivmed (ActionEvent e ){
While (true ){
Try {
Thread. currentThread (). sleep (100 );
} Catch (InterruptedException er ){}
If (flagTrue ){
System. out. println ("this is a test! ");
}
}
}

Void stopbutton_actionreceivmed (ActionEvent e ){
If (flagTrue) flagTrue = false;
Else flagTrue = true;
}
}


Summary:
In the example of not using a thread, when the start button is clicked, the entire Frame will no longer have any mouse events (such
Click stop and close );
In the example of using a thread, after you click the start button to start the thread, the Frame interface can properly handle other issues.
(For example, click stop to stop the thread. Click Close to exit the program)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.