使用JFrame,swing布局制作简易计算器
-实现加减乘除
-小数点运算暂时没有实现
-因为是用swing进行界面的布局,所以很方便。直接把控件布置好就可以了。主要是算法的问题。最大的难处就是如何从JTextField里获取两个数进行数学运算。
还有一个难处就是在计算器上进行连续运算。
以下是代码
package com.homework.lhh;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class Ex03 extends JFrame {
private static String strOne = "";// 存数字
private static String strTwo = "";// 存数字
private static double result;// 结果
private String operator = "";// 运算符号
private static double numOne;
private static double numTwo;
private JPanel contentPane;
private JTextField textLED;
private JButton btn0;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btnReset;// 清零
private JButton btnWait;// 等号
private JButton btnAdd;// 加号
private JButton btnMinus;// 减号
private JButton btnRide;// 乘号
private JButton btnDivide;// 除号
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Ex03 frame = new Ex03();
//设置居中
int w = (Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2;
int h = (Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT) / 2;
frame.setLocation(w, h);
frame.setVisible(true);
frame.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Ex03() {
setTitle("\u7B80\u5355\u5B9E\u73B0\u7684\u8BA1\u7B97\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, -57, 472, 546);
contentPane = new JPanel();
contentPane.setBackground(new Color(119, 136, 153));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textLED = new JTextField();
textLED.setFont(new Font("宋体", Font.PLAIN, 18));
textLED.setBounds(14, 13, 428, 55);
contentPane.add(textLED);
textLED.setColumns(10);
textLED.setHorizontalAlignment(JTextField.RIGHT);// 数字向右排列
btn1 = new JButton("1");
btn1.setBounds(14, 288, 97, 78);
contentPane.add(btn1);
btn2 = new JButton("2");
btn2.setBounds(125, 288, 97, 78);
contentPane.add(btn2);
btn3 = new JButton("3");
btn3.setBounds(236, 288, 97, 78);
contentPane.add(btn3);
btn4 = new JButton("4");
btn4.setBounds(14, 197, 97, 78);
contentPane.add(btn4);
btn5 = new JButton("5");
btn5.setBounds(125, 197, 97, 78);
contentPane.add(btn5);
btn6 = new JButton("6");
btn6.setBounds(236, 197, 97, 78);
contentPane.add(btn6);
btn7 = new JButton("7");
btn7.setBounds(14, 106, 97, 78);
contentPane.add(btn7);
btn8 = new JButton("8");
btn8.setBounds(125, 106, 97, 78);
contentPane.add(btn8);
btn9 = new JButton("9");
btn9.setBounds(236, 106, 97, 78);
contentPane.add(btn9);
btn0 = new JButton("0");
btn0.setBounds(125, 379, 97, 78);
contentPane.add(btn0);
btnReset = new JButton("\u6E05\u96F6");
btnReset.setBounds(14, 379, 97, 78);
contentPane.add(btnReset);
btnWait = new JButton("=");
btnWait.setBounds(236, 379, 97, 78);
contentPane.add(btnWait);
btnAdd = new JButton("+");
btnAdd.setBounds(347, 106, 97, 78);
contentPane.add(btnAdd);
btnMinus = new JButton("-");
btnMinus.setBounds(347, 197, 97, 78);
contentPane.add(btnMinus);
btnRide = new JButton("\u00D7");
btnRide.setBounds(347, 288, 97, 78);
contentPane.add(btnRide);
btnDivide = new JButton("\u00F7");
btnDivide.setBounds(347, 379, 97, 78);
contentPane.add(btnDivide);
JLabel lblNewLabel = new JLabel("\u5C0F\u602A\u517D");
lblNewLabel.setBounds(196, 468, 51, 18);
contentPane.add(lblNewLabel);
registerListeners();
}
// 注册监听器
public void registerListeners() {
BtnListener listener = new BtnListener();
btn1.addActionListener(listener);
btn2.addActionListener(listener);
btn3.addActionListener(listener);
btn4.addActionListener(listener);
btn5.addActionListener(listener);
btn6.addActionListener(listener);
btn7.addActionListener(listener);
btn8.addActionListener(listener);
btn9.addActionListener(listener);
btn0.addActionListener(listener);
btnAdd.addActionListener(listener);
btnMinus.addActionListener(listener);
btnRide.addActionListener(listener);
btnDivide.addActionListener(listener);
btnReset.addActionListener(listener);
btnWait.addActionListener(listener);
}
class BtnListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
if (e.getSource() != btnAdd && e.getSource() != btnMinus && e.getSource() != btnRide
&& e.getSource() != btnDivide && e.getSource() != btnReset && e.getSource() != btnWait) {
if (operator == "") {
strOne += ((JButton) e.getSource()).getText();
textLED.setText(strOne);
numOne = Double.parseDouble(strOne);
System.out.println("第一个数为:" + numOne);
} else {
strTwo += ((JButton) e.getSource()).getText();
textLED.setText(strTwo);
numTwo = Double.parseDouble(strTwo);
System.out.println("第二个数为:" + numTwo);
}
} else {
if (e.getSource() == btnAdd) {
if (strTwo != "") {
if (textLED.getText() != "") {
judgeSort();
} else {
result = numOne + numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
}
}
System.out.println("按下了加号");
System.out.println("当前结果为:" + result);
operator = "+";
}
if (e.getSource() == btnMinus) {
if (strTwo != "") {
if (textLED.getText() != "") {
judgeSort();
} else {
result = numOne - numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
}
}
System.out.println("按下了减号");
System.out.println("当前结果为:" + result);
operator = "-";
}
if (e.getSource() == btnRide) {
if (strTwo != "") {
if (textLED.getText() != "") {
judgeSort();
} else {
result = numOne * numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
}
}
System.out.println("按下了乘号");
System.out.println("当前结果为:" + result);
operator = "×";
}
if (e.getSource() == btnDivide) {
if (strTwo != "") {
if (textLED.getText() != "") {
judgeSort();
} else {
result = numOne / numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
}
}
System.out.println("按下了除号");
System.out.println("当前结果为:" + result);
operator = "÷";
}
if (e.getSource() == btnReset) {
strOne = "";
strTwo = "";
numOne = 0;
numTwo = 0;
result = 0;
operator = "";
textLED.setText("");
System.out.println("按下了清零号");
System.out.println("当前结果为:" + result);
}
if (e.getSource() == btnWait) {
if (operator == "+") {
result = numOne + numTwo;
}
if (operator == "-") {
result = numOne - numTwo;
}
if (operator == "×") {
result = numOne * numTwo;
}
if (operator == "÷") {
result = numOne / numTwo;
}
textLED.setText(result + "");
System.out.println("按下了等号");
System.out.println("当前结果为:" + result);
}
}
}
}
}
//该方法是实现连续运算的关键
//判断当前的符号,进行下一次运算
//比如:1+1-1 运算为 1+1=2 2-1=1 如果没有这个判断,这个运算将为:1+1-1=0 因为不会执行第一次加
public void judgeSort() {
if (operator == "+") {
result = numOne + numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
} else if (operator == "-") {
result = numOne - numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
} else if (operator == "×") {
result = numOne * numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
} else if (operator == "÷") {
result = numOne / numTwo;
numOne = result;
strOne = result + "";
strTwo = "";
textLED.setText("" + result);
}
}
}
运行结果如图: