2018-04-26

package cn.jhc;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class ClickListernerDemo extends JFrame implements ActionListener {

JPanel p;

JButton b;

JLabel l;

int count = 0;

public ClickListernerDemo() {

p = new JPanel();

b = new JButton("点击");

l = new JLabel(count + "次");

b.addActionListener(this);

p.add(l);

p.add(b);

this.add(p);

this.setSize(300, 100);

this.setLocation(300, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

ClickListernerDemo a = new ClickListernerDemo();

a.setVisible(true);

}

public void addActionListener() {

count++;

l.setText(count + "次");

}

@Override

public void actionPerformed(ActionEvent e) {

count++;

l.setText(count + "次");

}

}

你可能感兴趣的:(2018-04-26)