java模仿windows计算器示例
本文导语: 这个计算器的界面模仿Windows自带的简易计算器,包括交互界面和和对各种输入的响应。目前尚未实现菜单栏和记忆类按钮的功能 代码如下:import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import java.awt.Insets;import java.awt....
这个计算器的界面模仿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