//<applet code=Test.class width=300 height=200></applet>
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField text;
private JButton button1, button2, button3, exit_button;
private JPanel panel;
public Test(){
//Container con =getContentPane();
text = new JTextField(10);
button1 = new JButton("刘德华");
button2 = new JButton("张学友");
button3 = new JButton("蔡依林");
exit_button = new JButton("退 出");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
exit_button.addActionListener(this);
panel = new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
this.setLayout(new FlowLayout());
this.add(text);
this.add(panel);
this.add(exit_button);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == exit_button)
{
System.exit(0);
}
else
{
text.setText(e.getActionCommand());
}
}
public static void main(String args[]){
new Test();
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField text;
private JButton button1, button2, button3, exit_button;
private JPanel panel;
public Test(){
//Container con =getContentPane();
text = new JTextField(10);
button1 = new JButton("刘德华");
button2 = new JButton("张学友");
button3 = new JButton("蔡依林");
exit_button = new JButton("退 出");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
exit_button.addActionListener(this);
panel = new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
this.setLayout(new FlowLayout());
this.add(text);
this.add(panel);
this.add(exit_button);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == exit_button)
{
System.exit(0);
}
else
{
text.setText(e.getActionCommand());
}
}
public static void main(String args[]){
new Test();
}
}