62,614
社区成员
发帖
与我相关
我的任务
分享
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");
}
}
Action copyAction = new AbstractAction("Copy"){
public void actionPerformed(ActionEvent e){...}
};
JButton copyButton = new JButton(copyAction);
jtoolbar.add(copyAction);
jmenu.add(copyAction);
...
this.button1.Click += new System.EventHandler(this.button1_Click);
private void button1_Click(object sender, EventArgs e)
{
Application.ExitThread();
}