Java GUI (UI creation and listening mechanism)

Source: Internet
Author: User

/*
* GUI (graphical user Interface): Image user interface.
* CLI: Command user interface.
* Java provides GUI objects in java.awt and javaswing two packages.
* java. AWT: (abstract window ToolKit) Abstraction Windows Toolkit, need to invoke Local System method implementation functionality. belongs to the weight control.
* javax. Swing: On the basis of AWT, a set of graphical interface systems, which provide more components and are fully implemented by Java, enhance portability and belong to lightweight controls.
* Layout: The way the components in the container are discharged is the layout.
* 1.FlowLayout (Flow layout management)
* Left-to-right order, panel default layout manager.
* 2.BorderLayout (Border layout manager)
* The default layout manager for frame in the Cardinal.
* 3.GridLayout (Grid layout manager)
* The matrix of the rule.
* 4.CardLayout (Card layout manager)
* tab.
* 5.GridBagLayout (Grid package layout manager)
* Non-regular matrices.
*/
Import java.awt.*;
public class Guidemo {
public static void Main (string[] args) {
Awtdemo ();
System.out.println ("gogoing");
}
Define a form, set the form name, and set the form to be visible.
public static void Awtdemo ()
{
Set the form name,
Frame F=new Frame ("my AWT");
Set form visible
F.setvisible (TRUE);
}

}


/*
* Create a graphical interface:
* 1. Create a frame form.
* 2. Basic settings for the form, such as: size, location, layout.
* 3. Define the components.
* 4. Add the build to the form by using the form's Add method.
* 5. Let the form show through Setviseble (true).
*


* Two. Features of the event monitoring mechanism:
* 1. Event Source:
*Those graphical interface components in the AWT package or swing package.
* 2. Event:
*Each event source has its own unique corresponding events and common events.
* 3. Listener:
* The action (more than one action) that can start an event is already encapsulated in the listener.
* The above three are defined in Java, and get the object directly.
* 4. Event handling:
*The programmer himself needs to deal with the resulting action.
*
*/
Import java.awt.*;
Import java.awt.event.*;
public class Awtdemo {
public static void Main (string[] args) {
Framedemo ();
}
public static void Framedemo ()
{
Create a frame form.
Frame f=new frame ();
Basic settings for the form, such as: size, position, layout.
F.setsize (500,500);
F.settitle ("LOL client");
F.setlocation (300,300);
F.setlayout (New FlowLayout ());

Define components, and add properties.
Button B=new button ("I am a button");
F.add (b);


To invoke a method of adding a listener with a form, you must receive a (listener) Windowlisttener interface subclass object in the method.
In this interface there are seven methods, the subclass must be all replicated, and we just need to call the method we need, so this method does not work.
The abstract subclass of the interface Windowadapter all its methods, and is not abstract.
So we define subclasses of that class, and we can replicate the methods we need.
F.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
E represents all the information for an event, and it doesn't make much sense.
System.out.println ("Close WinDOS" +e.tostring ());
System.exit (0);

}


});
Make the form display
F.setvisible (TRUE);
}
}
/* Define the Windowadapter subclass.
Class Mywin extends Windowadapter
{
The form closes the action, as long as the listener hears the user's closing action automatically calls this method for processing.
public void windowclosing (WindowEvent e)
{
System.out.println ("Close WinDOS");
System.exit (0);
}
}*/

Import java.awt.*;
Import java.awt.event.*;
public class Framedemo {
You first define a reference to the component that you want in the drawing.
Private Frame F;
Private Button but;
Framedemo ()
{
Init ();
}
Initializes the graphical interface.
public void Init ()
{
F=new FRAME ("my frame");
Basic settings for frame, this method contains SetSize and setlocation.
F.setbounds (200,200,400,300);
F.setlayout (New FlowLayout ());
But=new button ("my button");
Add a build to a frame
F.add (But);
Before the form is displayed, load the events on the form.
MyEvent ();
Show form
F.setvisible (TRUE);
}
private void MyEvent ()
{
Add a listener to a form
F.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e)
{
System.exit (0);
}
});
Let the button have the function of exiting the program.
/*
* The button is the time source.
* then select which listener to view the functionality of the Component object.
* By looking at the button's API, the discovery buttons support a unique listening addActionListener
*/
To add an active listener to the button, the ActionListener listener does not need an adapter because it has only one method.
But.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
System.out.println ("Exit, Button dry");
System.exit (0);
}
});


}
public static void Main (string[] args) {
New Framedemo ();
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java GUI (UI creation and listening mechanism)

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.