日期:2014-05-20 浏览次数:20876 次
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Demo1 extends JFrame implements ActionListener {
JButton btn = new JButton("方法 1");
JButton btn2 = new JButton("方法 2");
JButton btn3 = new JButton("方法 3");
Cat cat = new Cat();
public static void main(String[] args)
{
Demo1 d1 = new Demo1();
}
public Demo1()
{
//方法1
this.btn.addActionListener(this);
//方法2
this.btn2.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showInputDialog("Btn2");
}
}
);
//方法3
this.btn3.addActionListener(this.cat);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.getContentPane().add(this.btn);
this.getContentPane().add(this.btn2);
this.getContentPane().add(this.btn3);
this.setVisible(true);
this.setSize(320,200);
this.setLocation(500,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn)
{
JOptionPane.showConfirmDialog(this, "Btn");
}
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showInputDialog("Btn3");
}
}