Labels and buttons are perhaps the two most common components of the graphical interface, and the buttons are always associated with triggering an action event.
Label
The label (JLabel) is the simplest swing component. The function of a label object is to describe the interface component that is located behind it. You can set the properties of a label, which is the foreground color, background color, font, and so on, but you cannot dynamically edit the text in the label.
The basic content of the program about the label has the following several aspects:
1. Declare a label name;
2. Create a Label object;
3. Add a Label object to a container.
The main construction methods of the JLabel class are:
1.JLabel (): Construct a label with no display text;
2.JLabel (String s): Constructs a label that displays text as s;
3.JLabel (String s, int align): Constructs a label that displays text as S. Align to display text horizontally, there are three ways to align: • left-aligned:
Jlabel.left
• Center alignment: Jlabel.center
• Right align: jlabel.right
Other common ways to JLabel classes are:
1.setText (String s): Sets 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): Sets the color of the text on the label, and the default color is black.
Button
The button (JButton) is used in the interface design to excite action events. The button can display text and can trigger an action event when the button is activated.
JButton Common construction methods are:
1.JButton (): Create a Button object without a caption;
2.JButton (String s): Creates a Button object with a caption of S.
Other common methods for JButton classes are:
1.setLabel (String s): Sets the caption text for the button.
2.getLabel (): Gets the caption text for the button.
3.setMnemonic (char mnemonic): Setting Hotkey
4.setToolTipText (String s): Set hint text.
5.setEnabled (Boolean B): setting whether to respond to events
6.setRolloverEnabled (Boolean B): sets whether to scroll.
7.addActionListener (ActionListener AL): Adds an action monitor to the button.
8.removeActionListener (ActionListener AL): The monitor that moves the button.
The basic contents of the Button handling action events are as follows:
1. The interface associated with the button action event is actionlistener, giving the definition of the class that implements the interface;
2. Declare a button name;
3. Create a Button object;
4. Add the button object to a container;
5. For the button object that needs to be controlled registers the monitor, implements listens to the event which produces on this button. If the class that contains the button object implements the monitoring interface, the code form of the registration monitor is
Copy Code code as follows:
addActionListener (this);
See example 11-3, if object A of Class A is a monitor, class A must implement the ActionListener interface, and the monitor registration requires two lines of code in the following form:
Copy Code code as follows:
A A = new A (); Create instance a of Class A
addActionListener (a); Monitor the event with object A as a monitor.
6. In the class that implements the interface ActionListener, give the definition of the method that handles the event:
Copy Code code as follows:
public void actionperformed (ActionEvent e);
In the method of handling the event, the event source information is obtained by means of obtaining the event source information, and the corresponding processing is judged and completed. The method of obtaining the event source is: Method GetSource () Gets the event source object; Method Getactioncommand () Gets the text information of the event source button.
Example 11-3 handles the button event instance, the application defines a window, the window has two buttons, and when the red button is clicked, the background color of the window is red; When you click the green button, the background color of the window is set to green.
Import Javax.swing.*;import Java.awt.*;import java.awt.event.*;
public class j503{public static void Main (String[]args) {Buttondemo mybuttongui=new Buttondemo ()//declaring and creating a button object
Mybuttongui.setvisible (TRUE);
} class Buttondemo extends JFrame implements actionlistener{public static final int width=250;
public static final int height=200;
Buttondemo () {setSize (width,height); Settitle ("button event sample");
Container Conpane=getcontentpane ();
Conpane.setbackground (Color.Blue);
Conpane.setlayout (New FlowLayout ());//Use FlowLayout layout JButton redbut=new JButton ("Red"); Redbut.addactionlistener (this);/to the red Button Register monitor conpane.add (redbut);//Add Red button to window JButton greenbut=new JButton ("Gree
n "); Greenbut.addactionlistener (this);/to the green Button Register monitor conpane.add (greenbut);//Add green button} public void Actionperfo in window
Rmed (ActionEvent e) {//Implement Interface handling event method Container Conpane=getcontentpane ();
if (E.getactioncommand (). Equals ("red"))//Is the Red button event Conpane.setbackground (color.red);
else if (E.getactioncommand (). Equals ("green"))//Is the Green button event Conpane.setbackground (Color.green);
Else{}}}
Using the mouse to click the button to produce an event object, the event delivered to the object, this process is called fire event. When an event is sent to a monitor object, the interface method implemented by the monitor object is invoked, and the system provides the parameters of the event object when invoked. The program does not have the code to invoke the Monitor method, but the program does two things: First, specify which object is the monitor, and it responds to the event triggered by the button, which is called the Monitor registration. Second, you must define a method that will be invoked when the event is sent to the monitor. The code for this method is not invoked in the program, and the call is executed by the system.
In the above program, the code
Redbut.addactionlistener (this);
Register this as the monitor for the Redbut button, and subsequent code also registers this as the monitor for the Greenbut button. In the above program, this is the current Buttondemo object Mybuttongui. In this way, the Buttondemo class is the class of the monitor object Mybuttongui as a two-button monitor. There is an implementation of the Monitor method in the class Buttondemo. When a button is clicked, the system automatically invokes the method actionperformed () as a parameter to the event's trigger.
Components are different, the types of events that are fired are different, and the type of monitor class is different. The event that the button fires is called an action event, and the corresponding monitor is called the action Monitor. The type of an action monitor object is ActionListener, and the class implements the ActionListener interface. The program embodies these content needs to do two points:
1. The first line of the class definition is connected with the code implements ActionListener;
2. Define method actionperformed () within the class.
The class Buttondemo in the previous program correctly did these two points.
Each interface element, when the event is fired, has a string that corresponds to this event, which is called the action command. You can get the command string for the action event parameter E with Code E.getactioncommand (), whereby the method actionperformed () can tell which button fired the event. By default, the command string for a button is the text on the button. You can use Method Setactioncommand () to set the command string for the interface component if necessary.
The above mentioned is the entire content of this article, I hope you can enjoy.