My first Java graphical user interface program

Source: Internet
Author: User

Java. I am a newbie and have recently started learning. Therefore, we often encounter many problems.

When writing the first Java graphical interface program, I saw on the Internet what SWT path should be configured. Originally, SWT plug-ins were used to optimize and visualize the graphic interface. For example, I wrote a program using the Java basic package and did not use the SWT plug-in. There is no need to configure the SWT path.

The following explains my first graphic interface program:

Displays the numbers of an integer. Demonstrate a complete application with a graphical user interface, including input data, display results, response events, and exception handling.

Package study;
Import java. AWT .*;
Import java. AWT. event .*;;
Public class digitframe extends frame implements actionlistener, windowlistener {
Private textfield text_input, text_hundred, text_decade, text_digit;
Private button button_ OK;
Private dialog;
Private Label label_dialog;
Public digitframe ()
{
Super ("show integer ");
This. setsize (190,150 );
This. setresizable (false); // The window size cannot be changed.
This. setbackground (Java. AWT. color. lightgray );
This. setlayout (New java. AWT. flowlayout (flowlayout. Left); // The stream layout is left aligned


This. Add (new label ("integer "));
Text_input = new textfield (10 );
This. Add (text_input );
Text_input.addactionlistener (this); // registers a click event listener event for a text row


Button_ OK = new button ("OK ");
This. Add (button_ OK );
Button_ OK .addactionlistener (this); // register the Click Event listener for the button


This. Add (new label ("Hundred bits "));
Text_hundred = new textfield (10 );
Text_hundred.seteditable (false); // only display, not edit
This. Add (text_hundred );


This. Add (new label ("Ten "));
Text_decade = new textfield (10 );
Text_decade.seteditable (false );
This. Add (text_decade );


This. Add (new label ("single digit "));
Text_digit = new textfield (10 );
Text_digit.seteditable (false );
This. Add (text_digit );


This. addwindowlistener (this );
This. setvisible (true );


Dialog = new dialog (this, "prompt", true); // mode window
Dialog. setsize (240,80 );
Label_dialog = new label ("", label. center); // the string of the label is empty and the center is aligned.
Dialog. Add (label_dialog );
Dialog. addwindowlistener (this); // registers the window event listener for the dialog box.
}
Public void actionreceivmed (actionevent e) // click the button and press enter in the text line.
{
Try
{
Final int I = integer. parseint (text_input.gettext ());
Text_hundred.settext ("" + (I/100); // hundreds of digits
Text_decade.settext ("" + (I/10% 10); // ten
Text_digit.settext ("" + (I % 10); // a single bit
}
Catch (numberformatexception NFE)
{
Label_dialog.settext ("/" "+ text_input.gettext () +"/"+" cannot be converted to an integer. Please enter it again! ");
Dialog. setlocation (this. getx () + 100, this. Gety () + 100 );
Dialog. setvisible (true );
}
Finally {}
}
Public void windowclosing (windowevent E)
{
If (E. getsource () = DIALOG)
Dialog. setvisible (false); // hide the dialog box
Else
System. Exit (0 );
}
Public void windowopened (invalid wevent e ){}
Public void windowactivated (windowevent e ){}
Public void windowdeactivated (receivwevent e ){}
Public void windowclosed (windowevent e ){}
Public void incluwiconified (incluwevent e ){}
Public void windowdeiconified (invalid wevent e ){}
Public static void main (string [] ARGs ){
New digitframe ();

}

}

The program design is described as follows:

Window close events cannot be delegated to other classes for processing

As the dialog box must also respond to window close events, in the windowclosing method body, you need to identify the current event source framework or dialog box, so the window close events in this example cannot be delegated to other classes for processing.

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.