还是不行啊,我不知道是不是我改的有问题,大家看看吧 帮我纠正以下。我在eclipse中编译都没通过。 它报错的是 java.lang.Error: Unresolved compilation problem: The type ActionListener cannot be instantiated at A.init(A.java:15) at A.main(A.java:30) 而且在btn.addActionListener(new ActionListener());中的ActionListener()有红色的下划线。 public class A extends JApplet implements ActionListener{ JTextField text; JButton btn; public void init() { text = new JTextField(10); btn = new JButton("确定"); getContentPane().setLayout(new FlowLayout()); getContentPane().add(text); getContentPane().add(btn); btn.addActionListener(new ActionListener()); } public void actionPerformed(ActionEvent e){ if(e.getSource()==btn) { text.setText("刚点击了,:"+btn.getText()); } }
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet a = new A(); frame.getContentPane().add(a); frame.setSize(200, 100); a.init(); a.start(); frame.setLocation(300,500);//居屏幕中央显示 frame.setVisible(true); } }
......
public void actionPerformed(ActionEvent e){
}
......
}
这样应该也可以。这就是把actionPerformed(AcionEvent e)写在外面。
帮我纠正以下。我在eclipse中编译都没通过。
它报错的是
java.lang.Error: Unresolved compilation problem:
The type ActionListener cannot be instantiated at A.init(A.java:15)
at A.main(A.java:30)
而且在btn.addActionListener(new ActionListener());中的ActionListener()有红色的下划线。
public class A extends JApplet implements ActionListener{
JTextField text;
JButton btn;
public void init() {
text = new JTextField(10);
btn = new JButton("确定");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(text);
getContentPane().add(btn);
btn.addActionListener(new ActionListener());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn) {
text.setText("刚点击了,:"+btn.getText());
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet a = new A();
frame.getContentPane().add(a);
frame.setSize(200, 100);
a.init();
a.start();
frame.setLocation(300,500);//居屏幕中央显示
frame.setVisible(true);
}
}
至于匿名类,只是一种用法。还可以用适配器的对吧