操作方法
打开eclipse软件,并且建立java一个工程,具体如下图:
声明,窗口类中的各个属性对象: private JPanel jp=new JPanel(); private JButton jb=new JButton(); private JLabel jl=new JLabel();
建立该类的构造函数,有下面代码可知,建立类一个事件监听器: public Testbutton(){ jp.add(jb); jp.add(jl); this.add(jp); this.setTitle("记录按钮就按下的次数"); jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Testbutton.this.jl.setText("按钮按下了"+(++count)+"次"); } }); this.setBounds(100, 100, 400, 120); this.setVisible(true); }
在main方法中实例化该窗口类: new Testbutton();
执行该程序查看结果,如图所示。