Tags and buttons may be the two most common components in the graphic interface. The buttons are always related to the excitation action event.
Label
Tags are the simplest SWING components. The role of the label object is to explain the interface component afterwards. You can set the attributes of the label, the foreground color, the background color, the font, etc., but you cannot dynamically edit the text in the label.
The basic content of the program about labels has the following aspects:
1. State a label name;
2. Create a tag object;
3. Add the tag object to a container.
The main constructor of the JLABEL class is:
1.jlabel (): Construct a label without display;
2.jlabel (string s): Construct a label with a display text as s;
3. Jlabel (String S, int Align): Construct a label with a display text as s. Align is the horizontal method of displaying text. There are three ways to align: • Left alignment:
Jlabel.left
• Center alignment: jlabel.center
• Right alignment: jlabel.richt
Other common methods of the JLABEL class are:
1.Settext (string s): Set the label display text;
2.gettext (): Get the label display text;
3.setbackground (color C): Set the background color of the label, the default background color is the background color of the container;
4.SetForeground (color C): Set the color of the text on the label, the default color is black.
Button
The button (JBUTTON) is used in the interface design to stimulate action events. The buttons can display text. When the button is activated, it can stimulate the mobilization event.
JBUTTON commonly used constructed methods are:
1.jbutton (): Create a button object without title;
2. JBUTTON (String S): Create a button object with a title S.
Other common methods of the jbutton class are:
1.Setlabel (String S): Set the title text of the button.
2.Getlabel (): The title text of the obtaining button.
3.Setmnemonic (Char Mnemonic): Set the hot key
4.Settooltiptext (string s): Set prompt text.
5.Setenabled (Boolean B): Set whether to respond to the event
6.Setrolloverenabled (Boolean B): Whether the settings can be rolled.
7.AdDactionListener (ActionListener Al): Add action monitor to the button.
8.RmoveActionListener (ActionListener Al): Monitor of the Move button.
There are several aspects of the basic content of the button to handle the action event:
1. The interface related to the button action event is ActionListener to give the definition of the class of the interface;
2. Declarize a button name;
3. Create a button object;
4. Add the button object to a container;
5. Register the monitor for the buttons that need to be controlled, and implement monitoring of events generated on this button. If it is the class where the button object is located to implement the monitoring interface, the code form of the registered monitor is
Copy code code as follows:
addactionListener (this);
See [Example 11-3], if it is an object of other category A as a monitor, the class A must implement the ActionListener interface, and the two lines of code need to be used to complete the monitor registration: the following forms:
Copy code code as follows:
A = new a (); // Create an instance A of Class A
addactionListener (a); // Use object A as a monitor to monitor the event.
6. In the implementation of interface ActionListener, the method of processing the processing event is given:
Copy code code as follows:
Public void ActionPerFormed (ActionEvent E);
In the method of processing the event, obtain the source information of the event by obtaining the source information of the event, and judge and complete the corresponding processing. The method of obtaining the event source is: method getsource () to obtain event source objects; method getActionCommand () gets text information of the source button of the event.
[Example 11-3] The processing button event instance, the application defines a window with two buttons in the window, when clicking the Red button, the background color of the window is placed in red; when clicking the Green button, the background color of the window is placed into green.
Import javax.swing.*; Import Java.awt.*; Import Java.awt.event.*; Public Class J503 {Public Static void Main (String [] ARGS) {Buttondemo Mybu ttongUI = New Buttondemo (); // Declaration and Create the button object mybuttongui.setvisible (true);}} Class Buttondemo Extends Jframe Implements ActionListener {Public Static Final Int Width = 250 C final height = 200; Buttondemo () {setsize (width, height); settitle ("button Event Sample "); Container Conpine = getContentPane (); Conpine.setbackGround (color.blue); conpane.setLayout (new flowLayout ()); // ton redbut = new jbutton ("red"); redbut. addactionListener (this); // Register the RED button to register the monitor Conpine.add (Redbut); // Add the red buttons in the window jbutton GreenBut = new jbutton ("Green"); GreenBut.AdDactionListener s); // Give the Green button Register Conpine.add (GreenBut); // Add the Green button} Public Void ActionPerFormed (ActionEvent E) {// ActionCommand (). Equals ("Red") // is the red buttons event Conpane.setbackGround (color.red); else if (e.getActionCommand (). Equals ("Green")) // is the Green button event .setBackground (color.gre.gre.gre.gre.gre EN ); Else {}}}
Use the mouse to click the button to generate an event object and send the event to the object. This process is called an inspiration event. When an event is sent to the monitor object, the interface method implemented by the monitor object is called, and the system will provide parameters of the event object when calling. Although there is no code for calling the monitor method in the program, the program does two things: First, which object specifies is the monitor, it responds to the incident of the stimulation of the button. This step is called the monitor registration. Second, a method must be defined. When the event is delivered to the monitor, this method will be called. The code is not called in the program. This call is performed by the system.
In the above program, code
Redbut.adDactionListener (this);
Registered this as a monitor of the Redbut button, and the subsequent code also registered this as a monitor of the GreenBut button. In the above programs, this is the current ButtonDemo object Mybuttongui. In this way, the ButtonDemo class is the class of the monitor object, and the object Mybuttongui is the monitor of the two buttons. There is a monitoring method in the class of buttondemo. When a button is clicked, the system uses the inspiration of the event as the parameter, and automatically call the method ActionPerformed ().
Different components, the types of incidents are different, and the types of monitors are different. The event stimulus event is called the Action event, and the corresponding monitor is called the ACTION monitor. The type of an Action monitor is ActionListener, and the class should implement the ActionListener interface. The program reflects these contents that require two points:
1. In the first line of the class definition, the code iMPLEMENTS ACTIONLISTENER;
2. Class definition method ACTIONPERFORMED ().
The class buttondemo in the previous program did these two points correctly.
When each interface element stimulates an event, a string corresponds to this event. This string is called the action command. Use code E.GetActionCommand () to obtain the command string of the Action Event Parameter E. Accordingly, the method can know which event is stimulated by the buttons. By default, the command string of the button is the text on the button. If necessary, you can use the method setActionCommand () to set the command string for the interface component.
The above is all the contents of this article. I hope everyone can like it.