如何在我的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);
}
}发布于 2014-02-11 05:58:11
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
}
}https://stackoverflow.com/questions/21694117
复制相似问题