Labels and buttons are probably the two most common components in the graphical interface, and the buttons are always related to firing action events.
Label
The label (JLabel) is the simplest swing component. The purpose of the label object is to describe the interface component that is located behind it. You can set the label's properties, that is, 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 aspects:
- declare a label name;
- Create a Label object;
- Joins a Label object to a container.
The main construction methods of the JLabel class are:
- JLabel (): Constructs a label with no display text;
- JLabel (String s): Constructs a label that displays the text as s;
- JLabel (String s, int align): Constructs a label that displays the text as S. Align is the horizontal way of displaying text, there are three ways to align it:
- Left-justified: Jlabel.left
- Center alignment: Jlabel.center
- Right-aligned: Jlabel.right
Other common methods of the JLabel class are:
- SetText (String s): Sets the label display text;
- GetText (): Gets the label display text;
- SetBackground (color C): Sets the background color of the label, the default background color is the background color of the container;
- Setforeground (color C): Sets the color of the text on the label, and the default color is black.
Button
button (JButton) is used to excite action events in the interface design. button to display text, which fires an action event when the button is activated.
JButton Common construction methods are:
- JButton (): Creates a Button object without a caption;
- JButton (String s): Creates a button object titled S.
Other common methods of the JButton class are:
- SetLabel (String s): Sets the caption text for the button.
- Getlabel (): Gets the caption text for the button.
- Setmnemonic (char mnemonic): Set Hotkey
- Settooltiptext (String s): Sets the hint text.
- SetEnabled (Boolean B): sets whether to respond to events
- Setrolloverenabled (Boolean B): sets whether scrollable.
- addActionListener (ActionListener AL): Adds an action monitor to the button.
- Removeactionlistener (ActionListener AL): The monitor that moves the button.
The basic contents of a Button handling action event are as follows:
- The interface associated with the button action event is ActionListener, which defines the class that implements the interface;
- declare a button name;
- Creates a button object;
- Adds a button object to a container;
- Register the monitor for the button object that you want to control, and listen for events that occur on this button. If the class that contains the button object implements the monitoring interface, the code form of the registration monitor is
1 addActionListener (this);
See example 11-3, if the object A of another class A as a monitor, class A must implement the ActionListener interface, complete the monitor registration requires the following form of two lines of code:
1 New A (); // creating instance A of Class A 2 addActionListener (a); // use Object A as a monitor to monitor events.
- In the class that implements the interface ActionListener, the definition of the method that handles the event is given:
1 Public void actionperformed (ActionEvent e);
In the method of handling the event, the event source information is obtained by obtaining the event source information, and the corresponding processing is judged and completed. The method for obtaining the source of the event is: Method GetSource () Gets the event source object; Method Getactioncommand () Gets the text information of the event source button.
1 "Example 11-3 "Handling button event instances, the application defines a window with two buttons in the window, and when the red button is clicked, the background color of the window is set to red;
2 Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.*;3 Public classj503{4 Public Static voidMain (String[]args) {5Buttondemo mybuttongui=NewButtondemo ();//declaring and creating a button object6Mybuttongui.setvisible (true);7 }8 }9 classButtondemoextendsJFrameImplementsactionlistener{Ten Public Static Final intwidth=250; One Public Static Final intheight=200; A Buttondemo () { -SetSize (Width,height); Settitle ("button Event Example"); -Container conpane=Getcontentpane (); the Conpane.setbackground (color.blue); -Conpane.setlayout (NewFlowLayout ());//Use FlowLayout layout -JButton redbut=NewJButton ("Red"); -Redbut.addactionlistener ( This);//register the Monitor with the red button +Conpane.add (redbut);//Add a red button to a window -JButton greenbut=NewJButton ("Green"); +Greenbut.addactionlistener ( This);//register the monitor with the green button AConpane.add (greenbut);//Add green button to window at } - Public voidactionperformed (ActionEvent e) {//ways to Implement interface handling events -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 in Conpane.setbackground (color.green); - Else{} to } +}
Use the mouse to click the button to generate the event object, the event is delivered to the object, this process is called the firing event. When an event is sent to a monitor object, the interface method implemented by the monitor object is called, and the system provides the parameters of the event object when called. Although there is no code to invoke the monitor method in the program, the program does two things: First, specify which object is the monitor, and it responds to the event that is fired by the button, which is called Monitor registration. Second, you must define a method that will be called when the event is sent to the monitor. There is no code in the program that calls this method, and the call is executed by the system.
In the program above, the code
Redbut.addactionlistener (this);
Register this as the monitor for the Redbut button, and the following code also registers this as the monitor for the Greenbut button. In the above program, this is the current Buttondemo object Mybuttongui. Thus, the Buttondemo class is the class of the monitor object, and the object Mybuttongui as a monitor for two buttons. There is an implementation of the Monitor method in the class Buttondemo. When a button is clicked, the system automatically calls the method Actionperformed () with the event's activator as the parameter.
Different components, the types of events that are fired are different, and the types of monitor classes are different. The event that the button fires is called the 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 things in order to achieve two points:
- The first line of the class definition is connected to the code implements ActionListener;
- The method actionperformed () is defined within the class.
The class Buttondemo in the previous program did exactly the two points.
Each interface element when the event is fired, there is a string corresponding to the event, which is called the action command. The command string for the action event argument e can be obtained with code E.getactioncommand (), whereby the method actionperformed () will know which button fired the event. By default, the command string for a button is the text on the button. If necessary, you can use the method Setactioncommand () to set the command string for the interface component.
Series Articles:
Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window Basics
Java know how much (82) Introduction to tags, buttons, and button events