registerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 创建新的忘记密码对象 ForgetPassword fp = new ForgetPassword(); } });换一种方式
时间: 2024-02-14 10:03:01 浏览: 27
你可以使用 Lambda 表达式来替换匿名内部类。
```
registerButton.addActionListener(e -> {
// 创建新的忘记密码对象
ForgetPassword fp = new ForgetPassword();
});
```
Lambda 表达式可以将一个方法作为一个参数传递给另一个方法。上面的代码使用了箭头符号 `->` 来表示 Lambda 表达式的参数和方法体,从而简化了匿名内部类的写法。
相关问题
分析一下这段代码oneButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player = 1; } }); twoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player = 2; } }); threeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player = 3; } });
这段代码是为三个按钮(oneButton、twoButton、threeButton)添加 Action 监听器,当用户点击按钮时,对应的 player 变量会被设置为相应的值(1、2、3)。具体来说,这里使用了匿名内部类 ActionListener 来实现监听器,重写了 actionPerformed 方法,在方法中设置 player 变量的值。这种方式可以避免创建多个类,使代码更加简洁。
public void actionPerformed(ActionEvent e) { jf.setVisible(false); new addFrame().setVisible(true); } }); btn2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.setVisible(false); new changeFrame().setVisible(true); } }); btn3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.setVisible(false); new DeleteFrame().setVisible(true); } }); btn4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.setVisible(false); new QueryFrame().setVisible(true); } }); } public static void main(String[] args) { GUI.GUIjiemian(); } }中的类和方法
这是一个 Java GUI 程序中的一个类,该类包含了一个窗口界面,其中有四个按钮,分别为添加、修改、删除和查询。每个按钮都绑定了一个 ActionListener 监听器,当用户点击按钮时,会触发对应的操作,如打开添加、修改、删除或查询窗口界面。
下面是该类的代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private JButton btn1, btn2, btn3, btn4;
private JPanel panel;
public MainFrame() {
setTitle("学生信息管理系统");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
btn1 = new JButton("添加");
btn2 = new JButton("修改");
btn3 = new JButton("删除");
btn4 = new JButton("查询");
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
add(panel);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new AddFrame().setVisible(true);
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new ChangeFrame().setVisible(true);
}
});
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new DeleteFrame().setVisible(true);
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new QueryFrame().setVisible(true);
}
});
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
```
其中,main 方法是程序的入口点,GUIjiemian 方法可能是您自己定义的方法,用于初始化界面和启动程序。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)