答案:import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Dictionary;import javax.swing.*;
public class Calculator extends JFrame {
private JTextField jtf;
private JPanel panel1, panel2, panel3, panel4;
private num numActionListener;
private JTextField jtf1;
private boolean IfResult = true;
private boolean point;
private String oper = "=";
private double result = 0;
private JMenuBar mybar;
private JMenu menu1, menu2, menu3;
private JMenuItem edititem1, edititem2, help1, help2, help3;
private JRadioButtonMenuItem seeitem1, seeitem2;
private JCheckBoxMenuItem seeitem3;
private ButtonGroup bgb;
private String back;
private DecimalFormat df;
private boolean Resultr = false;
private boolean flag = false;public Calculator(String title) {
super(title);
this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(4, 5, 5, 5));
panel3 = new JPanel(new BorderLayout(5, 5));
panel4 = new JPanel(new GridLayout(5, 1, 3, 5));df = new DecimalFormat("0.#####");
mybar = new JMenuBar();
menu1 = new JMenu("编辑(E)");
menu2 = new JMenu("查看(V)");
menu3 = new JMenu("帮助(H)");menu1.setMnemonic('E');
menu2.setMnemonic('V');
menu3.setMnemonic('H');menu1.setFont(new Font("宋体", Font.PLAIN, 12));
menu2.setFont(new Font("宋体", Font.PLAIN, 12));
menu3.setFont(new Font("宋体", Font.PLAIN, 12));edititem1 = new JMenuItem("复制(C)");
edititem2 = new JMenuItem("粘贴(P)");edititem1.setIcon(new ImageIcon("icons/copy.png"));
edititem2.setIcon(new ImageIcon("icons/paste.png"));bgb = new ButtonGroup();
seeitem1 = new JRadioButtonMenuItem("标准型(T)");
seeitem2 = new JRadioButtonMenuItem("科学型(S)");
seeitem3 = new JCheckBoxMenuItem("数字分组(I)");help1 = new JMenuItem("关于计算器(A)");
help2 = new JMenuItem("帮助主题(H)");help1.addActionListener(new clear());
help1.setIcon(new ImageIcon("icons/about.png"));
help2.setIcon(new ImageIcon("icons/help.png"));bgb.add(seeitem1);
bgb.add(seeitem2);menu1.add(edititem1);
menu1.add(edititem2);menu2.add(seeitem1);
menu2.add(seeitem2);
menu2.addSeparator();
menu2.add(seeitem3);menu3.add(help1);
menu3.addSeparator();
menu3.add(help2);mybar.add(menu1);
mybar.add(menu2);
mybar.add(menu3);
this.setJMenuBar(mybar);edititem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));edititem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
InputEvent.CTRL_MASK));seeitem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
InputEvent.CTRL_MASK));seeitem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));seeitem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
InputEvent.CTRL_MASK));help1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
help2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
InputEvent.CTRL_MASK));jtf = new JTextField(20);
jtf.setEditable(false);
jtf.setBackground(Color.white);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setText("0");
jtf.setBorder(BorderFactory.createLoweredBevelBorder());numActionListener = new num();
init();}
public void init() {
// panel 面板按钮的添加addButton(panel1, "Blackspace", new clear(), Color.red);
addButton(panel1, "CE", null, Color.red);
addButton(panel1, "C", new clear(), Color.red);addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "/", new signs(), Color.red);
addButton(panel2, "sqrt", new signs(), Color.blue);addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "*", new signs(), Color.red);
addButton(panel2, "%", new signs(), Color.blue);addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new signs(), Color.red);
addButton(panel2, "1/x", new signs(), Color.blue);addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new clear(), Color.blue);
addButton(panel2, ".", new dot(), Color.blue);
addButton(panel2, "+", new signs(), Color.red);
addButton(panel2, "=", new signs(), Color.red);JButton btns = new JButton("");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);
btns.setPreferredSize(new Dimension(10, 10));
// panel4面板按钮的添加
panel4.add(btns);
addButton(panel4, "MC", null, Color.red);
addButton(panel4, "MR", null, Color.red);
addButton(panel4, "MS", null, Color.red);
addButton(panel4, "M+", null, Color.red);
// 将面板添加倒顶层容器中panel3.add(panel1, BorderLayout.NORTH);
panel3.add(panel2, BorderLayout.CENTER);
add(jtf, BorderLayout.NORTH);
add(panel3);
add(panel4, BorderLayout.WEST);pack();
this.setResizable(false);
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// 按钮的添加方法
public void addButton(JPanel p, String name, ActionListener action, Color c) {
JButton btn = new JButton(name);
p.add(btn);
btn.setForeground(c);
btn.addActionListener(action);
}public void addButton(JPanel p, String name, ActionListener action) {
JButton btn = new JButton(name);
p.add(btn);
btn.addActionListener(action);
}public void getResult(double x) {
if (oper.equals("+")) {
result += x;
} else if (oper.equals("-"))
result -= x;
else if (oper.equals("*"))
result *= x;
else if (oper.equals("/"))
result /= x;
else if (oper.equals("="))
上一个:JAVA中的异常处理机制的原理
下一个:java 与数据库的问题??