JAVA计算器的图形界面应用程序
题目简介:
整体分析:
实验代码:
/*部分使用插件做界面*/ import java.awt.EventQueue; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.JTextField; import javax.swing.SwingConstants; import java.awt.Font; import javax.swing.JPanel; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.border.EmptyBorder; /** * @author 李祖林 * @Time 2017-6-22 */ public class Calcultor implements ActionListener { int i; int status = 0; // 保存运算符:0为空,1为加,2为减,3为乘,4为除 double sum1 = 0, sum2 = 0; // sum1为第一个数,也用于保存连续运算中的结果,sum2为第二个数 boolean key = false; // 判断是否有运算符 private JFrame frame; JButton[] bN = new JButton[10]; // bN为数字按钮,0-9分别表示0-9个数字 JButton[] bC = new JButton[6]; // bC为运算符按钮 private JTextField textField; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calcultor window = new Calcultor(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Calcultor() { frame = new JFrame(); frame.setBounds(100, 100, 510, 310); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); textField = new JTextField(); textField.setFont(new Font("宋体", Font.BOLD, 18)); textField.setHorizontalAlignment(SwingConstants.RIGHT); frame.getContentPane().add(textField, BorderLayout.NORTH); textField.setColumns(10); JPanel panel = new JPanel(); panel.setBorder(new EmptyBorder(20, 20, 20, 20)); // 设置panel的空白的上下左右间距为20 frame.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(new GridLayout(4, 4, 20, 20)); for (i = 0; i <= 9; i++) { bN[i] = new JButton(String.valueOf(i)); // String.valueOf(i)可以将其他类型数据转化为字符串 bN[i].addActionListener(this); } bC[0] = new JButton("C"); // bC[0]表示清空 bC[1] = new JButton("+"); // bC[1]表示加 bC[2] = new JButton("-"); // bC[2]表示减 bC[3] = new JButton("*"); // bC[3]表示乘 bC[4] = new JButton("/"); // bC[4]表示除 bC[5] = new JButton("="); // bC[5]表示等于 for (i = 0; i <= 5; i++) { bC[i].addActionListener(this); } panel.add(bN[9]); panel.add(bN[8]); panel.add(bN[7]); panel.add(bC[0]); panel.add(bN[6]); panel.add(bN[5]); panel.add(bN[4]); panel.add(bC[1]); panel.add(bN[3]); panel.add(bN[2]); panel.add(bN[1]); panel.add(bC[2]); panel.add(bN[0]); panel.add(bC[3]); panel.add(bC[4]); panel.add(bC[5]); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bC[0]) { sum1 = 0; sum2 = 0; status = 0; key = false; textField.setText(""); } if (key == false) { // 未按下运算符 for (i = 0; i <= 9; i++) { if (e.getSource() == bN[i]) { // 如果为数字按钮 sum1 = sum1 * 10 + i; textField.setText(String.valueOf(sum1)); } } for (i = 1; i <= 4; i++) { // 如果为控制按钮 if (e.getSource() == bC[i]) { status = i; // 保存运算符的状态 key = true; // 标记已按下运算符 } } } else { boolean tmp = false; for (i = 0; i <= 9; i++) { if (e.getSource() == bN[i]) { tmp = true; sum2 = sum2 * 10 + i; textField.setText(String.valueOf(sum2)); } } if (tmp == false) { switch (status) { case 1: sum1 = sum1 + sum2; break; case 2: sum1 = sum1 - sum2; break; case 3: sum1 = sum1 * sum2; break; case 4: sum1 = sum1 / sum2; break; } for (i = 1; i <= 4; i++) { if (e.getSource() == bC[i]) { status = i; sum2 = 0; } } if (e.getSource() == bC[5]) { status = 0; textField.setText(String.valueOf(sum1)); sum1 = 0; sum2 = 0; key = false; } } } } }
实验结果:
2017-06-22