我也是初学者,不过我建议你用swing的组件,下面是我写得代码import java.awt.event.*;
import java.awt.*;
import javax.swing.*;public class A extends JApplet{
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.setLocationRelativeTo(null);//居屏幕中央显示
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;public class A extends JApplet{
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.setLocationRelativeTo(null);//居屏幕中央显示
frame.setVisible(true);
}
}
a.java:4: A is not abstract and does not override abstract method actionPerforme
d(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class A extends Applet implements ActionListener
^
怎么修改我的那个啊?谢谢了.
楼上的源码我抄下了,下线了研究研究,呵呵,谢谢你的帮助.
这是你写的,我估计是打字错误,没有重载actionPerformed(ActionEvent e)方法,当然报错
public void actionPerformed(ActionEvent e){}
楼主些错了吧,我调试过了,成功!