日期:2014-05-20 浏览次数:20799 次
import java.awt.*; import java.awt.event.*; public class Cen { public static void main(String args[]) { Frame f=new Frame("Test"); final Button b=new Button("press me"); f.setLayout(new FlowLayout()); f.add(b); f.setSize(200,100); f.setVisible(true); class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Action occurred"); if(e.getSource().equals(b)) System.out.println("sdf"); } } } }
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class Cen { private static JButton b; public static void main(String args[]) { JFrame f = new JFrame("Test"); b = new JButton("press me"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b.addActionListener(new Cen().new ButtonHandler()); f.setLayout(new FlowLayout()); f.add(b); f.setSize(200, 100); f.setVisible(true); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Action occurred"); if(e.getSource().equals(b)) { System.out.println("sdf"); } } } }
------解决方案--------------------
内部类??
------解决方案--------------------
还是用swing的组件比较好,直到awt组件的事件模型就够了,基本没有什么地方会使用到awt了。