java计算器 java模仿windows计算器示例
人气:0这个计算器的界面模仿Windows自带的简易计算器,包括交互界面和和对各种输入的响应。目前尚未实现菜单栏和记忆类按钮的功能
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicBorders;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
Container c = getContentPane();
StringBuilder number1 = new StringBuilder("");// 储存第1个数字字符串
StringBuilder number2 = new StringBuilder("");// 储存第2个数字字符串
StringBuilder operator = new StringBuilder("");// 储存运算符
StringBuilder result = new StringBuilder("");// 储存运算结果
JTextField numbershow = new JTextField("0.");// 数字显示区域,初始显示为"0."
public static void main(String[] args) {// 主方法
Calculator c1 = new Calculator();
c1.setVisible(true);
}
public Calculator() {// 计算器构造方法
setTitle("计算器");
setBounds(100, 100, 260, 245);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenuBar();// 创建菜单栏
createNumberShow();// 创建数字显示区域
createMemoShow();// 创建记忆显示
createClearButtons();// 创建Backspace,CE,C等三个按钮
createButtonArea();// 创建数字按钮区域
}
private void createMenuBar() {// 创建菜单栏的方法
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu menu1 = new JMenu("编辑");
JMenu menu2 = new JMenu("查看");
JMenu menu3 = new JMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
JMenuItem menu1item1 = new JMenuItem("复制");
JMenuItem menu1item2 = new JMenuItem("粘贴");
JMenuItem menu2item1 = new JMenuItem("标准型");
JMenuItem menu2item2 = new JMenuItem("科学型");
JMenuItem menu2item3 = new JMenuItem("科学分组");
JMenuItem menu3item1 = new JMenuItem("帮助主题");
JMenuItem menu3item2 = new JMenuItem("关于计算器");
menu1.add(menu1item1);
menu1.add(menu1item2);
menu2.add(menu2item1);
menu2.add(menu2item2);
menu2.add(menu2item3);
menu3.add(menu3item1);
menu3.add(menu3item2);
}
private void createNumberShow() {// 创建数字显示区域的方法
numbershow.setHorizontalAlignment(JTextField.RIGHT);
numbershow.setBounds(5, 0, 245, 22);
numbershow.setEnabled(false);
numbershow.setDisabledTextColor(Color.BLACK);
c.add(numbershow);
}
private void createMemoShow() {// 创建记忆显示的方法
JTextField memoshow = new JTextField();
memoshow.setEditable(false);
memoshow.setBounds(10, 30, 28, 22);
memoshow.setBorder(new BasicBorders.FieldBorder(Color.black,
Color.black, Color.white, Color.white));
c.add(memoshow);
}
private void createClearButtons() {// 创建Backspace,CE,C等三个按钮的方法
JButton[] clearbutton = new JButton[3];
String[] clearbuttontext = { "Backspace", "CE", "C" };
for (int i = 0; i < 3; i++) {
clearbutton[i] = new JButton();
clearbutton[i].setText(clearbuttontext[i]);
clearbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
clearbutton[i].setMargin(new Insets(0, 0, 0, 0));
clearbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
clearbutton[i].setForeground(Color.blue);
clearbutton[i].setBounds(48 + i * 68, 30, 63, 22);
clearbutton[i].setForeground(Color.red);
c.add(clearbutton[i]);
}
clearbutton[0].addActionListener(new ActionListener() {// 为Backspace按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 如果未做任何输入
showNumber(numbershow, number1);// 显示number1
} else if (operator.toString().equals("")) {// 如果只输入了number1
number1.deleteCharAt(number1.toString().length() - 1);// 将number1的最后一个字符去掉
showNumber(numbershow, number1);// 显示number1
} else if (number2.toString().equals("")) {// 如果只输入了number1和operator
showNumber(numbershow, number1);// 不作任何处理,显示number1
} else {// 如果输入了number1、operator、number2
number2.deleteCharAt(number2.toString().length() - 1);// 将number2的最后一个字符去掉
showNumber(numbershow, number2);// 显示number2
}
}
});
clearbutton[1].addActionListener(new ActionListener() {// 为CE按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 如果未做任何输入
showNumber(numbershow, number1);// 显示number1
} else if (operator.toString().equals("")) {// 如果只输入了number1
number1.setLength(0);// 清除number1
showNumber(numbershow, number1);// 显示number1
} else if (number2.toString().equals("")) {// 如果输入了number1和operator
showNumber(numbershow, number2);// 不作任何处理,显示number2
} else {// 如果输入了number1、operator、number2
number2.setLength(0);// 清除number2
showNumber(numbershow, number2);// 显示number2
}
}
});
clearbutton[2].addActionListener(new ActionListener() {// 为C按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {// 将所有储存清零
number1.setLength(0);
number2.setLength(0);
operator.setLength(0);
numbershow.setText("0.");
result.setLength(0);
}
});
}
private void createButtonArea() {// 创建数字按钮区域的方法
JPanel ButtonArea = new JPanel();
ButtonArea.setBounds(5, 55, 245, 125);
ButtonArea.setLayout(new GridLayout(4, 6, 5, 5));
c.add(ButtonArea);
JButton[] numberbutton = new JButton[24];
String[] numberbuttontext = { "MC", "7", "8", "9", "/", "sqrt", "MR",
"4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/X", "M+",
"0", "+/-", ".", "+", "=" };
for (int i = 0; i < 24; i++) {// 使用循环为这24个按钮添加标识
numberbutton[i] = new JButton(numberbuttontext[i]);
ButtonArea.add(numberbutton[i]);
if (i % 6 == 0 || i % 6 == 4 || i == 23) {// 操作符按钮设置为红色
numberbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
numberbutton[i].setMargin(new Insets(0, 0, 0, 0));
numberbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
numberbutton[i].setForeground(Color.red);
} else {// 其它设置为蓝色
numberbutton[i].setHorizontalAlignment(SwingConstants.CENTER);
numberbutton[i].setMargin(new Insets(0, 0, 0, 0));
numberbutton[i].setFont(new java.awt.Font("Arial", 0, 9));
numberbutton[i].setForeground(Color.blue);
}
}
int[] numbers = { 19, 13, 14, 15, 7, 8, 9, 1, 2, 3 };// 该数组中的数字分别代表0-9等数字在numberbuttontext数组中序号
for (int i = 0; i <= 9; i++) { // 使用循环为这0-9这十个数字按钮添加监听器
final String str = String.valueOf(i);
numberbutton[numbers[i]].addActionListener(new ActionListener() {// 为0-9按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 没有输入operator之前
add(number1, str); // 只设置number1的值
showNumber(numbershow, number1);// 只显示number1的值
} else {// 输入operator之后
add(number2, str);// 只设置number2的值
showNumber(numbershow, number2);// 只显示number2的值
}
}
});
}
numberbutton[20].addActionListener(new ActionListener() { // 为"+/-"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 没有输入operator之前
add(number1, "+/-");// 只设置number1的值
showNumber(numbershow, number1);// 只显示number1的值
} else {// 输入operator之后
add(number2, "+/-");// 只设置number2的值
showNumber(numbershow, number2);// 只显示number2的值
}
}
});
numberbutton[21].addActionListener(new ActionListener() { // 为"."按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (operator.toString().equals("")) {// 在输入operator之前,只显示number1的值
add(number1, ".");
showNumber(numbershow, number1);
} else {// 在输入operator之后,只显示number2的值
add(number2, ".");
showNumber(numbershow, number2);
}
}
});
numberbutton[22].addActionListener(new ActionListener() { // 为"+"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("+");
}
});
numberbutton[16].addActionListener(new ActionListener() { // 为"-"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("-");
}
});
numberbutton[10].addActionListener(new ActionListener() { // 为"*"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("*");
}
});
numberbutton[4].addActionListener(new ActionListener() { // 为"/"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
operator.setLength(0);
operator.append("/");
}
});
numberbutton[23].addActionListener(new ActionListener() { // 为"="按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 当number1为空时
showNumber(numbershow, number1);
} else if (operator.toString().equals("")) {// 当number1不为空,而operator为空时
showNumber(numbershow, number1);
} else if (number2.toString().equals("")) {// 当number1、operator均不为空,而number2为空时
switch (operator.toString()) {
case ("+"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
+ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("-"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
- Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("*"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
* Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("/"): {
number2.append(number1.toString());
double d = Double.parseDouble(number1
.toString())
/ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
}
} else {// 当number1、operator、number2均不为空时
switch (operator.toString()) {
case ("+"): {
double d = Double.parseDouble(number1
.toString())
+ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("-"): {
double d = Double.parseDouble(number1
.toString())
- Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("*"): {
double d = Double.parseDouble(number1
.toString())
* Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
case ("/"): {
double d = Double.parseDouble(number1
.toString())
/ Double.parseDouble(number2.toString());
result.setLength(0);
result.append(d);
showNumber(numbershow, result);
number1.setLength(0);
number1.append(d);
}
break;
}
}
}
});
numberbutton[17].addActionListener(new ActionListener() { // 为"1/x"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 没有输入number1时
numbershow.setText("除数不能为零");
} else if (operator.toString().equals("")) {// 输入了number1,但没有输入operator
if (Double.parseDouble(number1.toString()) == 0) {// 如果number1的值为零
numbershow.setText("除数不能为零");
} else {// 如果number1的值不为零
double d = 1 / (Double.parseDouble(number1
.toString()));
number1.setLength(0);
number1.append(d);// 将number1的值开放并存储
showNumber(numbershow, number1);
}
} else if (number2.toString().equals("")) {// 输入了number1、operator,但没有输入number2
double d = 1 / (Double.parseDouble(number1
.toString()));
number2.append(d);// 将number1的值开放并存储
showNumber(numbershow, number2);
} else {// 输入了number1、operator,number2
double d = 1 / (Double.parseDouble(number2
.toString()));
number2.setLength(0);
number2.append(d);// 将number2的值开放并存储
showNumber(numbershow, number2);
}
}
});
numberbutton[11].addActionListener(new ActionListener() { // 为"%"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
// 暂时空缺
}
});
numberbutton[5].addActionListener(new ActionListener() { // 为"sqrt"按钮添加监听器
@Override
public void actionPerformed(ActionEvent e) {
if (number1.toString().equals("")) {// 没有输入number1时
showNumber(numbershow, number1);
} else if (operator.toString().equals("")) {// 输入了number1,但没有输入operator
if (Double.parseDouble(number1.toString()) < 0) {// number1小于0
numbershow.setText("函数输入无效");
} else {// number1大于0
double d = Math.sqrt(Double.parseDouble(number1
.toString()));
number1.setLength(0);
number1.append(d);// 将number1的值开放并存储
showNumber(numbershow, number1);
}
} else if (number2.toString().equals("")) {// 输入了number1、operator,但没有输入number2
double d = Math.sqrt(Double.parseDouble(number1
.toString()));
number2.append(d);// 将number1的值开放并存储
showNumber(numbershow, number2);
} else {// 输入了number1、operator、number2
double d = Math.sqrt(Double.parseDouble(number2
.toString()));
number2.setLength(0);
number2.append(d);// 将number2的值开放并存储
showNumber(numbershow, number2);
}
}
});
}
public void add(StringBuilder s1, String s2) {// 定义按钮输入后数字字符串变化的方法
if (s2.equals("+/-")) {// 定义输入"+/-"后数字字符串的变化
if (s1.toString().equals("") || s1.toString().equals("0")) {// 如果数字字符串为空或者0,那么不发生变化
s1.append("");
} else {// 如果数字字符串不为空也不为0,那么在数字字符串前增加或删除"-"字符
if (s1.toString().startsWith("-")) {
s1.deleteCharAt(0);
} else {
s1.insert(0, "-");
}
}
}
if (s2.equals(".")) {// 定义输入"."后数字字符串的变化
if (s1.toString().indexOf(".") == -1) {// 查找数字字符串中是否含有"."字符,如果没有则执行以下代码
if (s1.toString().equals("")) {// 如果数字字符串为空,那么将数字字符串设置为"0."
s1.setLength(0);
s1.append("0.");
} else {
s1.append(".");
}
} else {// 如果有,则不发生变化
s1.append("");
}
}
if (s2.equals("0")) {// 定义输入"0"后数字字符串的变化
if (s1.toString().equals("0")) {// 当数字的字符串为"0"时,不发生变化
s1.append("");
} else {// 当数字的字符串吧为"0"时,在其字符串后增加"0"
s1.append("0");
}
}
for (int i = 1; i < 10; i++) {// 通过循环,定义输入1-9后数字字符串的变化
String str = String.valueOf(i);
if (s2.equals(str)) {// 定义输入1-9后数字字符串的变化
if (s1.toString().equals("0")) {
s1.setLength(0);
s1.append(str);
} else
s1.append(str);
}
}
}
public void showNumber(JTextField j, StringBuilder s) {// 定义数字显示区域如何显示数字字符串的方法
if (s.toString().equals("") == true || s.toString().equals("0") == true) {
j.setText("0.");
} else if (s.toString().indexOf(".") == -1) {
j.setText(s.toString() + ".");
} else {
j.setText(s.toString());
}
}
}
加载全部内容