import java.awt.*; import java.awt.event.*; public class Test { public static void main(String args[]){ Frame f = new Frame(); Button b = new Button("按钮"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("按下了"); } }); f.add(b); f.setSize(100,100); f.show(); } }
这么简单的问题还要分,可恶 下面例子比较适合集中处理命令 import java.awt.*; import java.awt.event.*; public class Test implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("按下了"+((Button)e.getSource()).getText()); } public static void main(String args[]){ Frame f = new Frame(); Button b = new Button("按钮"); b.addActionListener(this); f.add(b); f.setSize(100,100); f.show(); } }
import java.awt.event.*;
public class Test
{
public static void main(String args[]){
Frame f = new Frame();
Button b = new Button("按钮");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("按下了");
}
});
f.add(b);
f.setSize(100,100);
f.show();
}
}
下面例子比较适合集中处理命令
import java.awt.*;
import java.awt.event.*;
public class Test implements ActionListener
{
public void actionPerformed(ActionEvent e) {
System.out.println("按下了"+((Button)e.getSource()).getText());
}
public static void main(String args[]){
Frame f = new Frame();
Button b = new Button("按钮");
b.addActionListener(this);
f.add(b);
f.setSize(100,100);
f.show();
}
}