Home >Java >javaTutorial >Introduction to tags, buttons and button events based on java

Introduction to tags, buttons and button events based on java

高洛峰
高洛峰Original
2017-01-22 16:47:391326browse

Labels and buttons are perhaps the two most common components in graphical interfaces, and buttons are always related to triggering action events.

Label

Label (JLabel) is the simplest Swing component. The function of the label object is to describe the interface component located behind it. You can set the properties of the label, that is, the foreground color, background color, font, etc., but you cannot dynamically edit the text in the label.

The basic contents of the program regarding tags include the following aspects:
1. Declare a tag name;
2. Create a tag object;
3. Add the tag object to a certain container.

The main construction methods of the JLabel class are:
1.JLabel (): Construct a label with no displayed text;
2.JLabel (String s): Construct a label with the displayed text s ;
3.JLabel(String s, int align): Construct a label whose text is s. align is the horizontal way to display text. There are three alignment methods: •Left alignment:

JLabel.LEFT

•Center alignment: JLabel.CENTER
•Right alignment: JLabel .RIGHT

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

Button (JButton) is used to trigger action events in interface design. Buttons can display text and fire action events when the button is activated.

Commonly used JButton construction methods are:
1.JButton(): Create a button object without a 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(): Get the title text of the button.
3.setMnemonic(char mnemonic): Set the hot key
4.setToolTipText(String s): Set the prompt text.
5.setEnabled(boolean b): Set whether to respond to events
6.setRolloverEnabled(boolean b): Set whether it can be scrolled.
7.addActionListener(ActionListener aL): Add an action monitor to the button.
8.removeActionListener(ActionListener aL): Move the monitor of the button.

The basic contents of button processing action events include the following aspects:

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

addActionListener(this);

, see [Example 11-3]. If object a of another class A is used as a monitor, class A must implement ActionListener interface, two lines of code in the following form are required to complete monitor registration:

A a = new A();  //创建类A的实例a
addActionListener(a);  //用对象a作为监视器对事件进行监视。

6. In the class that implements the interface ActionListener, give the definition of the method for processing events:

public void actionPerformed(ActionEvent e);

In In the method of processing the event, the method of obtaining the event source information is used to obtain the event source information, and the corresponding processing is judged and completed. The methods to obtain the event source are: method getSource() to obtain the event source object; method getActionCommand() to obtain the text information of the event source button.

[Example 11-3] Example of processing button events. The application defines a window and sets two buttons in the window. When the Red button is clicked, the background color of the window is set to red; click Green When the button is clicked, 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();//声明并创建按钮对象
    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("按钮事件样例");
    Container conPane=getContentPane();
    conPane.setBackground(Color.BLUE);
    conPane.setLayout(new FlowLayout());//采用FlowLayout布局
    JButton redBut=new JButton("Red");
    redBut.addActionListener(this);//给Red按钮注册监视器
    conPane.add(redBut);//在窗口添加Red按钮
    JButton greenBut=new JButton("Green");
    greenBut.addActionListener(this);//给Green按钮注册监视器
    conPane.add(greenBut);//在窗口添加Green按钮
  }
  public void actionPerformed(ActionEvent e){//实现接口处理事件的方法
    Container conPane=getContentPane();
    if(e.getActionCommand().equals("Red"))//是Red按钮事件
      conPane.setBackground(Color.RED);
    else if(e.getActionCommand().equals("Green"))//是Green按钮事件
      conPane.setBackground(Color.GREEN);
    else{}
  }
}

Click the button with the mouse to generate an event object and deliver the event to the object. This process is called firing the event. When an event is sent to the 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 call the monitor method in the program, the program does two things: First, it specifies which object is the monitor, and it will respond to events triggered by the button. This step is called monitor registration. Second, you must define a method that will be called when an event is sent to the monitor. There is no code to call this method in the program, this call is executed by the system.

In the above program, the code
redBut.addActionListener(this);
registers this as the monitor of the redBut button, and the subsequent code also registers this as the monitor of 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, and the object MyButtonGUI serves as the monitor of the 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 initiator as a parameter.

Different components have different types of events triggered and different types of monitor classes. The event triggered by the button is called an action event, and the corresponding monitor is called an action monitor. The type of an action monitor object is ActionListener, and the class must implement the ActionListener interface. The program needs to do two things to reflect these contents:

1. Connect the code implements ActionListener to the first line of the class definition;
2. Define the method actionPerformed () within the class.

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

When each interface element fires an event, there is a string corresponding to the event. This string is called an action command. Use the code e.getActionCommand() to get the command string of the action event parameter e. Based on this, the method actionPerformed() can know which button triggered the event. By default, the button's command string 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 the entire content of this article, I hope you all like it.

For more articles related to the introduction of tags, buttons and button events based on Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn