java吧 关注:1,247,191贴子:12,727,181
  • 2回复贴,共1

不继承JFrame的类如何实现对其成员变量button1的时间监听器代码如下

只看楼主收藏回复


1楼
import java.awt.*;
import javax.swing.*;
class Deng{
public JFrame jf = new JFrame("酒店管理系统");
public Container con = jf.getContentPane() ;
public Toolkit toolkit = Toolkit.getDefaultToolkit() ;
public Dimension sc = toolkit.getScreenSize() ;
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) ;
button1.setSize(90,25) ;
button1.setLocation(80,180 ) ;
button2.setSize(90,25) ;
button2.setLocation(220, 180) ;
con.add(button1) ;
con.add(button2) ;
con.setBackground(Color.black) ;
jf.setResizable(false) ;
jf.setVisible(true) ;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



IP属地:湖南1楼2011-11-03 15:40回复
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //实现侦听器接口
    class Deng implements ActionListener{
    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() throws Exception{
    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);
    //添加指定的动作侦听器,以接收发自此按钮的动作事件
    button1.addActionListener(this);
    button2.addActionListener(this);
    con.add(name1);
    con.add(pass1);
    con.add(textName);
    con.add(textPs);
    con.add(button1);
    con.add(button2);
    con.setBackground(Color.black);
    //系统默认外观
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    jf.setResizable(false);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    //处理动作事件
    public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();
    if(obj==button1){
    JOptionPane.showMessageDialog(jf, "欢迎光临,请问你是要住店呢,还是要吃饭啊?");
    }
    if(obj==button2){
    JOptionPane.showMessageDialog(jf, "很抱歉, 我们暂时未提供重置服务");
    }
    }
    }
    public class Denglu {
    public static void main(String args[]) throws Exception{
    new Deng();
    }
    }
    


    2楼2011-11-03 17:06
    回复
      这种写法我也试过,但好像不行。。我改成另外一种写法了:
      class ButtonListener implements ActionListener{
      public actionPerformed(ActionEvent e)
      {
      }
      }
      class Deng {
      .........
      button1.addActionListener(new ButtonListener());
      ...........
      }


      IP属地:湖南3楼2011-11-03 21:17
      回复