使用来自另一个Java类的 actionPerformed
Java中的事件处理使用动作侦听器可能很容易实现,但是当您在一个Java类文件中有许多动作侦听器并希望将设置的 GUI 部分(主要用于设置框架和其他对象)与ActionListener 在您的类文件中,但它比您想象的要简单让我们先看看将 Actionlistener 实现到Java类的步骤。 ActionListener 是“ Java.util.event”包的一部分,它只有一个方法actionPerformed(ActionEvent)在用户刚刚执行操作时调用。
过程:实现ActionListener的步骤如下:
- 步骤 1:创建事件处理程序类并指定该类要么实现 ActionListener 接口,要么扩展实现 ActionListener 接口的类。
- 第 2 步:将事件处理程序类的实例注册为一个或多个组件的侦听器。
- 第 3 步:在侦听器接口中包含实现方法的代码。
- 第 4 步:现在使用称为actionPerformed(ActionEvent o) 的 ActionListener 方法创建文件。
实现:让我们创建第一个包含所有框架和其他组件的Java文件。
第1步:
public class ActionListenerClass implements ActionListener {
第2步:
someComponent.addActionListener(instanceOfactionlsitenerclass);
button.addActionListener(instanceOfactionlistenerclass)
// It could be a button or any other component
第 3 步:
For example:
public void actionPerformed(ActionEvent e) {
...// code that reacts to the action...
// The single argument to the method is an ActionEvent object
// that gives information about the event
}
示例 1-A:
Java
// Java Program demonstrating the use of
// action listener from another java file
// Importing Swing class
import java.awt.event.ActionListener;
// Importing ActionListner from awt package
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
// Class for actionlistner
public class GFG
// Creating a new text field
{
static JTextField textfield = new JTextField();
// Main driver method
public static void main(String[] args)
{
// Creating a button with no text and caption
JFrame frame = new JFrame();
// Creating a button with the specified iccon object
JButton button = new JButton("button!");
// Setting frame size using setSize(),setLayout(),
// setBounds
// Setting width and height of a frame
// using setSize() method
frame.setSize(375, 250);
frame.setLayout(null);
button.setBounds(40, 50, 100, 20);
textfield.setBounds(40, 20, 150, 20);
// Component added to frame
frame.add(button);
frame.add(textfield);
// Object of type actionlistener is created
// with reference of actionperformclass
ActionListener listener = new actionperformclass();
// Adding the instance of event handler
// as listener of component
button.addActionListener(listener);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Java
// Java Program that creates the file with the method
// actionPerformed(ActionEvent o) of ActionListener
// Importing awt module and Swing class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// Class
// An action listener that prints a message
public class actionperformclass
extends classactionlistener implements ActionListener {
// Method
public void actionPerformed(ActionEvent event)
{
// settext of textfield object of Jtextfield
textfield.setText("button is clicked");
}
}
第 4 步:现在使用 ActionListener 的 actionPerformed(ActionEvent o)函数创建文件。
例1(B)
Java
// Java Program that creates the file with the method
// actionPerformed(ActionEvent o) of ActionListener
// Importing awt module and Swing class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// Class
// An action listener that prints a message
public class actionperformclass
extends classactionlistener implements ActionListener {
// Method
public void actionPerformed(ActionEvent event)
{
// settext of textfield object of Jtextfield
textfield.setText("button is clicked");
}
}
输出:上面两个程序会由类actionListener生成它是驱动类
The flow is when the user clicks the Button, the button fires an action event which invokes the action listener’s actionPerformed method. Each time the user presses the button, the message is displayed in the text field.
Note: The class file of program should be present of both the java file.