1、打开eclipse软件,并且建立java一个工程,具体如下图:
2、声明,窗口类中的各个属性对象: p筠续师诈rivate JPanel jp=new JPanel(); private JButton jb=荏鱿胫协new JButton(); private JLabel jl=new JLabel();
3、建立该类的构造函数,有下面代码可知,建立类一个事件监听器: 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); }
4、在main方法中实例化该窗口类:new Testbutton();
5、执行该程序查看结果,如图所示。