gpt4 book ai didi

java - Action 监听器,执行 Gui

转载 作者:行者123 更新时间:2023-12-02 06:13:16 25 4
gpt4 key购买 nike

如何在我的 gui 程序上放置 actionListenerActionEvent 因为我无法放置按钮的功能,请帮助我,这是迄今为止我的代码。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import javax.swing.*;
public class jiw extends JFrame
{
private JTextField aw1;
private JLabel aww;
private JButton aw2;

public jiw()
{
setLayout(new FlowLayout());


aww = new JLabel("Enter Your Password");
add(aww);

aw1 = new JTextField(15);
add(aw1);

aw2 = new JButton("Enter");
add(aw2);
setVisible(true);

actionPerformed(ActionEvent e)
{
Object source = e.getSource();

if(source == aw2)
{
System.exit(0);
}
}





}
public static void main(String args [])
{
jiw v = new jiw();

v.setSize(200,200);
v.setResizable(false);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

最佳答案

首先实现 ActionListener,然后覆盖actionPerformed(ActionEvent e)

public class JavaApplication1 extends JFrame implements ActionListener
{
private JTextField aw1;
private JLabel aww;
private JButton aw2;

public JavaApplication1()
{
setLayout(new FlowLayout());

aww = new JLabel("Enter Your Password");
add(aww);

aw1 = new JTextField(15);
add(aw1);

aw2 = new JButton("Enter");
add(aw2);
setVisible(true);

}
public static void main(String args [])
{
JavaApplication1 v = new JavaApplication1();

v.setSize(200,200);
v.setResizable(false);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent e) {
// Add your functionality here
}
}

关于java - Action 监听器,执行 Gui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21694117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com