A brief introduction to the Java basics of tags buttons and button events

  • 2020-04-01 03:54:40
  • OfStack

Labels and buttons are perhaps the two most common components of a graphical interface, and buttons are always associated with triggering action events.

The label

JLabel is the simplest Swing component. The tag object is used to describe the interface component that is located behind it. You can set the attributes of a tag, such as the foreground color, background color, font, etc., but you cannot dynamically edit the text in a tag.

The basic content of the program about the label has the following aspects:
Declare a tag name;
2. Create a label object;
Add a label object to a container.

The main constructor of the JLabel class is:
1.JLabel () : construct a label with no text;
2.JLabel (String s) : construct a label with text s;
3.JLabel(String s, int align) : construct a label with text s. Align is the horizontal display of text, there are three kinds of alignment: • Left:

JLabel. LEFT

The & # 8226; CENTER alignment: jlabel.center
The & # 8226; RIGHT alignment: jlabel.right

Other common methods of the JLabel class are:
1. SetText (String s) : sets the label to display text;
GetText () : gets tag display text;
3. SetBackground (Color c) : sets the background Color of the label, and 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

Jbuttons are used in interface design to trigger action events. Buttons can display text and fire action events when the button is activated.

Common construction methods of JButton are:
1.JButton() : creates a button object without a title;
2.JButton(String s) : creates a button object titled s.

Other common methods of the JButton class are:
1. SetLabel (String s) : sets the title text of the button.
2. GetLabel () : gets the title text of the button.
SetMnemonic (char mnemonic) : sets the hotkey
SetToolTipText (String s) : sets the prompt text.
SetEnabled (Boolean b) : sets whether to respond to an event
6. SetRolloverEnabled (Boolean b) : sets whether it is scrollable.
7. AddActionListener (ActionListener aL) : add an action monitor to the button.
8. RemoveActionListener (ActionListener aL) : move the monitor of the button.

The basic content of button handling action events has the following aspects:

1. The interface related to button action events is ActionListener, and the definition of the class that implements the interface is given;
2. Declare a button name;
3. Create a button object;
4. Add the button object to a container;
5. Register the monitor for the button object to be controlled, and monitor the events generated on the button. If the class where the button object resides implements the monitoring interface, the code for the registration monitor is


addActionListener(this);

See [example 11-3]. If the object A of class A is used as the monitor, class A must implement the ActionListener interface. To complete the registration of the monitor, two lines of code are required as follows:


A a = new A();  //Creates an instance of class A A
addActionListener(a);  //Events are monitored using object a as the monitor. < br / >

6. In the class that implements the interface ActionListener, the definition of the method to handle the event is given:


public void actionPerformed(ActionEvent e);

In the method of processing events, the method of obtaining the event source information is used to obtain the event source information, and judge and complete the corresponding processing. Methods to get the event source are: method getSource() to get the event source object; The getActionCommand() method gets the text information for the event source button.

Handle button event instance, the application program defines a window, set two buttons in the window, when the Red button is clicked, the background color of the window is set to 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();//Declare and create the 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(" Sample button event ");
     Container conPane=getContentPane();
     conPane.setBackground(Color.BLUE);
     conPane.setLayout(new FlowLayout());//Use FlowLayout layout
     JButton redBut=new JButton("Red");
     redBut.addActionListener(this);//Register the monitor for the Red button
     conPane.add(redBut);//Add the Red button to the window
     JButton greenBut=new JButton("Green");
     greenBut.addActionListener(this);//Register the monitor for the Green button
     conPane.add(greenBut);//Add the Green button to the window
   }
   public void actionPerformed(ActionEvent e){//Implement methods for interfaces to handle events
     Container conPane=getContentPane();
     if(e.getActionCommand().equals("Red"))//Is the Red button event
       conPane.setBackground(Color.RED);
     else if(e.getActionCommand().equals("Green"))//It's a Green button event
       conPane.setBackground(Color.GREEN);
     else{}
   }
 }

The process of generating an event object by clicking a button with the mouse and delivering the event to the object is called a fire event. When an event is sent to the monitor object, the interface methods implemented by the monitor object are called, and the system provides the parameters of the event object. There is no code in the program to call the monitor method, but the program does two things: first, specify which object is the monitor, and it will respond to the event fired by the button. This step 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 to call this method; 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 the subsequent code 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 for the monitor object, and the object MyButtonGUI ACTS as the monitor for the two buttons. There is an implementation of the monitor method in the ButtonDemo class. When a button is clicked, the system automatically calls the method actionPerformed (), taking the initiator of the event as an argument.

Different components fire different kinds of events, and different types of monitor classes. The event that the button fires is called an action event, and the corresponding monitor is called an action monitor. An action monitor object is of type ActionListener, and the class implements the ActionListener interface. The program needs to do two things to reflect these contents:

1. Implements ActionListener on the first line of a class definition;
Define the method actionPerformed () in the class.

The ButtonDemo class in the previous program does both of these things correctly.

Each interface element, when firing an event, has a string corresponding to that event, called the action command. The command string for the action event parameter e is obtained by using code such that the method actionPerformed() knows which button fired the event. By default, the command string for the button is the text on the button. If necessary, set the command string for the interface component using the method setActionCommand().

The above is all the content of this article, I hope you can enjoy it.


Related articles: