//邓盼文2010/12/28修改设计
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//________________________________________
public class Calculator extends JFrame implements ActionListener
{//(主方法)
JMenuBar menuBar;//菜单栏的实现。将 JMenu
对象添加到菜单栏以构造菜单。
JMenu menu,menu1;//菜单的该实现是一个包含 JMenuItem
的弹出窗口
JMenuItem menuItem, menuItem1;//弹出窗口
//JButton e.getSource() ;//【】
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal,
changePN, clear; // 运算符
private JButton[] numbers; // 数字
//_____________________________构造函数_________________
Calculator() {
createMenu();//添加菜单栏
setTitle("Calculator");
dotExist = false; //【 初始化变量】 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("加");
minus = new JButton("减");
multi = new JButton("乘");
div = new JButton("除");
sqrt = new JButton("开平方");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("归零");
//
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
getContentPane().setForeground(new
java.awt.Color(255,0,255));
this.setFocusTraversalPolicyProvider(true);
this.setSize(193, 205);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 计算部分【对按钮进行监听】————————————————————————————————
public void actionPerformed(ActionEvent e) {
//e.getSource() = (JButton)
e.getSource();//_________
//
if(e.getSource()== menuItem1){// String message,title;//用来存储帮助信息,字符串类型
title=new String("关于作者");
message=new
String("制作者:09软工二班邓盼文");///____________________________0000
int
messagetype=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(this,message,title,messagetype);
}
//
if (e.getSource() == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (e.getSource() == equal) {
operate('=');
equaled = true;
} else if (e.getSource() == plus) {
operate('+');
equaled = false;
} else if (e.getSource() == minus) {
operate('-');
equaled = false;
} else if (e.getSource() == multi) {
operate('*');
equaled = false;
} else if (e.getSource() == div) {
operate('/');
equaled = false;
} else if (e.getSource() == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (e.getSource() == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (e.getSource() == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText("" + i);
else if (!operated)
operation.setText(operation.getText() + i);
else {
operation.setText("" + i);
operated = false;
}
}
if (e.getSource() == dot &&
!dotExist) {
operation.setText(operation.getText() + ".");
dotExist = true;
}
//
//*/
}
}
//_______
private void operate(char operator) {
double currentNumber =
Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' &&
equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
//计算部分
// 网格布局管理区【GridBagLayout】
private void addComponent(GridBagLayout layout, Component
component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}
//添加菜单
void createMenu() {
menuBar = new JMenuBar(); // 新建菜单栏
setJMenuBar(menuBar);
menu = new JMenu("项目名称"); //
创建第一个菜单(在面板上显示)
menuBar.add(menu);//添加弹出窗口
menuItem = new JMenuItem("简易计算器", new
ImageIcon("images/img.gif"));
menu.add(menuItem);//弹窗要一个一个地添加,否则会覆盖的
menu1 = new
JMenu(" ");
menuBar.add(menu1);//添加弹出窗口
menu1.setText("帮助");
menuItem1 = new JMenuItem("制作者
");
//menuItem1 = new JMenuItem("邓盼文制作", new
ImageIcon(" "));//【对话框】
menu1.add(menuItem1);
menuItem1 .addActionListener(this);//_对
menuItem1注册监听器____________
}
//createMenu()
//_____________________________main
_____________________________
public static void main(String[] args) {
Calculator calc = new Calculator ();
calc.setSize(290, 400);//窗口大小
calc.setVisible(true);//窗口可见
}
}