import javax.swing.*;
import java.awt.GridLayout;
class WindowUI implments {
JFrame frame;
JPanel panel;
JButton but1, but2;
JTextField tf;
public WIndowUI() {
frame = new JFrame("WindowUI");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout(0,1));
but1 = new JButton("Button1");
but1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tf.setText("1");
}
});
but2 = new JButton("Button2");
but2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tf.setText("2");
}
tf = new JTextField();
panel.add(but1);
panel.add(but2);
panel.add(tf);
frame.add(panel);
fame.setVisible(true);
frame.pack();
});
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class AwtEvent extends JFrame implements ActionListener { //文件名为:AwtEvent.java
private JButton but1, but2;
private JTextField text;
public AwtEvent() {
super("AwtEvent");
Container c = getContentPane();
c.setLayout(new FlowLayout());
text = new JTextField(10);
but1 = new JButton("1");
but2 = new JButton("2");
c.add(text);
c.add(but1);
c.add(but2);
setSize(300, 80);
setVisible(true);
validate();
but1.addActionListener(this);
but2.addActionListener(this);
}
public static void main(String[] args) {
new AwtEvent();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but1) {
text.setText("1");
} else if (e.getSource() == but2) {
text.setText("2");
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonAction {
private JFrame frame;
private JPanel panel;
private JButton b1,b2;
private JTextField t;
public ButtonAction(){
frame=new JFrame("按钮事件");
panel=new JPanel();
b1=new JButton("1");
b2=new JButton("2");
t=new JTextField(10);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
t.setText("1");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
t.setText("2");
}
});
panel.add(t);
panel.add(b1);
panel.add(b2);
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new ButtonAction();
}
}
这都要别人写,太对不起自己的学费了。