import javax.swing.JApplet;
import javax.swing.*;
import java.awt.*;
public class MyApplet extends JApplet {
private JButton b1 = new JButton("button1");
private JButton b2 = new JButton("button2");
private JLabel l = new JLabel("please chose button");
public void init() {
ActionListener a1 = new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
l.setText("button b1 is press!");
}
if (e.getSource() == b2) {
l.setText("button b2 is press!");
}
}
};

b1.addActionListener(a1);
b2.addActionListener(a1);
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
setLayout(new BorderLayout());
add(p,BorderLayout.CENTER);
//add(1,BorderLayout.C);
}
}

解决方案 »

  1.   

    头文件错了
    不好意思
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class MyApplet extends JApplet {
    private JButton b1 = new JButton("button1");
    private JButton b2 = new JButton("button2");
    private JLabel l = new JLabel("please chose button");
    public void init() {
    ActionListener a1 = new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b1) {
    l.setText("button b1 is press!");
    }
    if (e.getSource() == b2) {
    l.setText("button b2 is press!");
    }
    }
    };

    b1.addActionListener(a1);
    b2.addActionListener(a1);
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());
    p.add(b1);
    p.add(b2);
    setLayout(new BorderLayout());
    add(p,BorderLayout.CENTER);
    //add(1,BorderLayout.C);
    }
    }