importjava.awt.*;importjava.awt.event.*;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjavax.swing.*;publicclassListenerimplementsAction...
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;
public class Listener implements ActionListener {
private JFrame frame;
private JTextField tf;
private JButton[] allButtons;
private JButton a;
private JButton b;
private JButton c;
public static void main(String[] args) {
Listener two = new Listener();
two.init();
}
public void init() {
frame = new JFrame("Calculator");
Container contentPane = frame.getContentPane();
contentPane.add(new Label("计算器"), BorderLayout.NORTH);
frame.setLayout(new BorderLayout());
//Container contentPane = frame.getContentPane();
JPanel centerPanel =new JPanel();
JPanel northPanel =new JPanel();
JPanel southPanel =new JPanel();
southPanel.setLayout(new GridLayout(4,4));
tf = new JTextField(30);
centerPanel.add(tf);
for (int i = 0; i < allButtons.length; i++) {
allButtons[i].addActionListener(this);}
a = new JButton("words");
b = new JButton("show");
c = new JButton("change");
allButtons = new JButton[16];
String str = "123+456-789*0.=/";
for (int i = 0; i < 16; i++) {
allButtons[i] = new JButton(str.substring(i, i + 1));
}
southPanel.setLayout(new GridLayout(4, 4));
northPanel.setLayout(new GridLayout(1, 3));
frame.add(southPanel, BorderLayout.SOUTH);
for (int i = 0; i < allButtons.length; i++) {
southPanel.add(allButtons[i]);
}
northPanel.add(a);
northPanel.add(b);
northPanel.add(c);
frame.add(southPanel,BorderLayout.CENTER);
frame.add(northPanel,BorderLayout.SOUTH);
contentPane.add(tf, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setVisible(true);
// 注册监听程序
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String s = "This is my first java program and thank you for your support";
tf.setText(s);
c.setBackground(Color.CYAN);
}
}
展开