Java-running program piece

Source: Internet
Author: User

Run the program piece and familiarize yourself with some basic event mechanisms through the process of human-computer interaction.

Import java. AWT .*;

Import java. AWT. event .*;

Import java. Applet .*;

 

Class statictest {

// Static global variables

Static int COUNT = 0;

Public statictest (){

++ Count;

System. Out. println ("count:" + count );

}

}

 

Public class test extends applet {

Private button BTN = new button ("CREATE ");

Private textfield TF = new textfield (10 );

 

// Anonymous internal class

Class create implements actionlistener {

Public void actionreceivmed (actionevent e ){

// Each time an object is instantiated, the value of count is accumulated, instead of starting from 0 again.

Statictest ST = new statictest ();

}

}

 

@ Override

Public void Init (){

Add (BTN );

// Add a click event

BTN. addactionlistener (New create ());

Add (TF );

}

 

Public static void main (string [] ARGs ){

Test T = new test ();

Frame frame = new frame ();

// Add a close event to the window

Frame. addwindowlistener (

New actionadapter (){

Public void windowclosing (windowevent e ){

System. Exit (0 );

}

}

);

Frame. Add (T, boderlayout. center );

Frame. setsize (300,200 );

// Initialize controls in the window

T. INIT ();

T. Start ();

// Display the program chip window

Frame. setvisible (true );

}

}

 

// Multithreading

Import java. AWT .*;

Import java. AWT. event .*;

Import java. Applet .*;

 

Public class test extends applet {

Private button start = new button ("START"),

Stop = new button ("stop ");

Private textfield TF = new textfield (10 );

Private runflag = true;

Private Static int COUNT = 0;

Private threadtest TT = NULL;

// Use threadtest as an anonymous internal class

Private threadtest extends thread {

// Start the thread when constructing the threadtest sample object

Public threadtest (){

Start ();

}

@ Override

Public void run (){

While (true ){

Try {

Thread. currentthread (). Sleep (200 );

} Catch (interruptedexception e ){

E. printstacktrace ();

}

If (runflag ){

TF. settext (integer. tostring (++ count ));

}

}

}

}

 

Class start implements actionlistener {

@ Override

Public void actionreceivmed (actionevent e ){

If (TT = NULL)

Tt = new threadtest ();

}

}

 

Class stop implements actionlistener {

@ Override

Public void actionreceivmed (actionevent e ){

If (TT! = NULL)

Runflag =! Runflag;

}

}

 

@ Override

Public void Init (){

Add (STOP );

Stop. addactionlistener (new stop ());

Add (start );

Start. addactionlistener (New Start ());

Add (TF );

}

 

Public static void main (string [] ARGs ){

Frame frame = new frame ("program piece ");

Test T = new test ();

Frame. addwindowlistener (

New windowadapter (){

@ Override

Public void windowclosing (windwoevent e ){

System. Exit (0 );

}

}

);

Frame. Add (T, borderlayout. center );

Frame. setsize (300,200 );

T. INIT ();

T. Start ();

Frame. setvisible (true );

}

}

 

// Merge threads in the main class

To merge threads and main classes, you must implement the runnable interface and override the run () method. However, this is very different from the classes inherited from threads. To open a new thread, you only need to pass in the main class as a parameter (type traceability (runnable ))

Import java. AWT .*;

Import java. AWT. event .*;

Import java. Applet .*;

 

// Implement the runnable interface

Public class test extends applet implements runnable {

 

Private button start = new button ("START "),

Stop = new button ("stop ");

Private textfield TF = new textfield (10 );

Private thread th = NULL;

Private Boolean runflag = true;

Private Static int COUNT = 0;

 

// Override the run Method

@ Override

Public void run (){

While (true ){

Try {

Th. Sleep (200 );

} Catch (interruptedexception e ){

E. printstacktrace ();

}

If (runflag)

TF. settext (integer. tostring (++ count ));

}

}

 

Class start implements actionlistener {

@ Override

Public void actionreceivmed (actionevent e ){

If (Th = NULL ){

// Input the current context object, that is, the upstream type runnable (because this interface is implemented)

Th = new thread (test. This );

Th. Start ();

}

}

}

 

Class stop implements actionlistener {

@ Override

Public void actionreceivmed (actionevent e ){

Runflag =! Runflag;

}

}

 

@ Override

Public void Init (){

Add (start );

Start. addactionlistener (New Start ());

Add (STOP );

Stop. addactionlistener (new stop ());

Add (TF );

}

 

Public static void main (string [] ARGs ){

Frame frame = new frame ("program piece ");

Test T = new test ();

Frame. Add (T, borderlayout. center );

Frame. addwindowlistener (

New windowadapter (){

@ Override

Public void windowclosing (windwoevent e ){

System. Exit (0 );

}

}

);

Frame. setsize (300,200 );

T. INIT ();

T. Start ();

Frame. setvisible (true );

}

}

 

// Basic Knowledge: static variables cannot be declared in anonymous internal classes.

 

 

/** -- Note --**/

The above code is written in the text editor and may be omitted.

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.