日期:2014-05-20  浏览次数:20665 次

怎样给其中的button1和button2添加监听事件监听器?
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class Deng{
public JFrame jf = new JFrame("酒店管理系统");
public Container con = jf.getContentPane() ;
public Toolkit toolkit = Toolkit.getDefaultToolkit() ;
public Dimension sc = toolkit.getScreenSize() ;
public JLabel name1 = new JLabel("用户名") ;
public JLabel pass1 = new JLabel("密码") ;
public JTextField textName = new JTextField() ;
public JPasswordField textPs = new JPasswordField() ;
public JButton button1 = new JButton("登陆") ;
public JButton button2 = new JButton("重置") ;
public Deng() {
con.setLayout(null) ;
jf.setSize(sc.width/3,sc.height*10/25) ;
jf.setLocation(sc.width/3,sc.height/4) ;
con.setLayout(null) ;
jf.setSize(sc.width/3,sc.height*10/25) ;
jf.setLocation(sc.width/3,sc.height/4) ;
jf.setResizable(false) ;
name1.setLocation(80,30) ;
name1.setSize(100, 100) ;
name1.setForeground(Color.white) ;
pass1.setLocation(80,90) ;
pass1.setSize(100,100) ;
pass1.setForeground(Color.white) ;
textName.setSize(140, 20) ;
textName.setLocation(170, 70) ;
textPs.setSize(140, 20) ;
textPs.setLocation(170, 130) ;
textPs.setEchoChar('*') ;
button1.setSize(90,25) ;
button1.setLocation(80,180 ) ;
button2.setSize(90,25) ;
button2.setLocation(220, 180) ;
con.add(name1) ;
con.add(pass1) ;
con.add(textName) ;
con.add(textPs) ;
con.add(button1) ;
con.add(button2) ;
con.setBackground(Color.black) ;
jf.setResizable(false) ;
jf.setVisible(true) ;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Denglu {
public static void main(String args[]) {
new Deng() ;
}
}


------解决方案--------------------
Java code
        button1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //...
                
            }
        });
        button2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //...
                
            }
        });

------解决方案--------------------
楼上正解
------解决方案--------------------
探讨
Java code

button1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
//...
……