package chapter11_3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private String[] str = { "7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "%",
"1", "2", "3", "-", "1/x",
"0", "+/-", ".", "+", "=" };
private JTextField tf_out;
private JButton jb_bk, jb_ce, jb_c;
private JButton[] jb_key;
private char ch = '#';
private boolean can = false;
private double num1;
public void creatGUI() {
tf_out = new JTextField();
tf_out.setHorizontalAlignment(JTextField.RIGHT);
tf_out.setColumns(18);
tf_out.setEditable(false); // 设置输出不可编辑
tf_out.setText("0");
this.add(tf_out, BorderLayout.NORTH);
JPanel p = new JPanel(new BorderLayout(3, 8));
JPanel p1 = new JPanel(new GridLayout(1, 3, 3, 10));
p.add(p1, "North");
jb_bk = new JButton("Backspace");
jb_bk.setForeground(Color.RED);
jb_bk.addActionListener(this);
jb_ce = new JButton("CE");
jb_ce.setForeground(Color.RED);
jb_ce.addActionListener(this);
jb_c = new JButton("C");
jb_c.setForeground(Color.RED);
jb_c.addActionListener(this);
p1.add(jb_bk);
p1.add(jb_ce);
p1.add(jb_c);
JPanel p2 = new JPanel(new GridLayout(4, 5, 3, 3));
p.add(p2, BorderLayout.CENTER);
jb_key = new JButton[str.length];
for (int i = 0; i < str.length; i++) {
jb_key[i] = new JButton(str[i]);
jb_key[i].addActionListener(this);
if (i == 3 || i == 8 || i == 13 || i == 18 || i == 19) {
jb_key[i].setForeground(Color.RED);
} else {
jb_key[i].setForeground(Color.BLUE);
}
p2.add(jb_key[i]);
}
this.add(p, BorderLayout.CENTER);
this.setTitle("计算器");
this.setIconImage(new ImageIcon("image/1.jpg").getImage()