........
public class TestApplet extends JApplet {
........
JTextField jtxt = new JTextField();
JTextArea jta = new JTextArea();
JButton jbtn = new JButton();
........
public TestApplet() {
......
registerListener();
}
private void registerListener() {
SymAction sym = new SymAction();
jtxt.addActionListener(sym);
jbtn.addActionListener(sym);
}private class SymAction implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent event) {
Object object = event.getSource();
if (object == jtxt) jta.setText(jtxt.getText());
else if (object == jbtn) jta.setText(jtxt.getText());
}
}
}
public class TestApplet extends JApplet {
........
JTextField jtxt = new JTextField();
JTextArea jta = new JTextArea();
JButton jbtn = new JButton();
........
public TestApplet() {
......
registerListener();
}
private void registerListener() {
SymAction sym = new SymAction();
jtxt.addActionListener(sym);
jbtn.addActionListener(sym);
}private class SymAction implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent event) {
Object object = event.getSource();
if (object == jtxt) jta.setText(jtxt.getText());
else if (object == jbtn) jta.setText(jtxt.getText());
}
}
}
例子:
public void AppletClass implements EventListener{
……
……
public void EventListenerMethod(EventListener event){
这里写上将要由事件产生的目标代码
}public init(){EventListener.add(要事件来监听的控件);
}
}
大概就是这样了……很久没有写过applet了……代码可能有错误,建议你搜一下,网上有很多的教程的……
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Frame1 extends JFrame {
JPanel contentPane;
JTextField jTextField1 = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JButton jButton1 = new JButton(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(7, 26, 110, 22));
contentPane.setLayout(null);
this.setResizable(false);
this.setSize(new Dimension(387, 243));
this.setTitle("Frame Title");
jScrollPane1.setBounds(new Rectangle(162, 24, 195, 180));
jTextArea1.setText("");
jButton1.setBounds(new Rectangle(13, 120, 73, 25));
jButton1.setText("导入");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jScrollPane1, null);
contentPane.add(jTextField1, null);
contentPane.add(jButton1, null);
jScrollPane1.getViewport().add(jTextArea1, null);
} //Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e) {
jTextArea1.append(jTextField1.getText() + "\n");
jTextField1.setText("");
jTextField1.requestFocus();
}
}class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}