如何在我的gui程序中放置actionListener
或ActionEvent
,因为我无法使用我的按钮功能请帮助我这是我的代码到目前为止。
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);
}
}
答案 0 :(得分:2)
implements
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
}
}