@FishC1通过该代码对你所书写代码进行优化import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Calculator extends JFrame{
//定义JTextField 显示的字符串
String string = new String();
public Calculator(){
// 创建一个 Container 容器并初始化。
Container container=getContentPane();
//设置容器布局:网格布局样式,5行1列,水平间距50,垂直间距10
container.setLayout(new GridLayout(5,1,50,10));
// 创建一个 JTextField 对象并初始化。 JTextField 是用于显示操作和计算结果的文本框。
// 参数 26 表明可以显示 26 列的文本内容
JTextField result_TextField = new JTextField(26);
//把字符串赋值给文本框
result_TextField.setText(string);
//创建一个带文本的按钮-->清除键
JButton clear_Button = new JButton(" clear ");
//给按钮添加事件监听器
clear_Button.addActionListener(clearButtonListener(result_TextField));
//数字键0到9
JButton button0 = new JButton("0");
button0.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button1 = new JButton("1");
button1.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button2 = new JButton("2");
button2.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button3 = new JButton("3");
button3.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button4 = new JButton("4");
button4.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button5 = new JButton("5");
button5.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button6 = new JButton("6");
button6.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button7 = new JButton("7");
button7.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button8 = new JButton("8");
button8.addActionListener(numbersAndOperatorsListener(result_TextField));
JButton button9 = new JButton("9");
button9.addActionListener(numbersAndOperatorsListener(result_TextField));
//计算命令按钮 加减乘除以及小数点等
//小数点
JButton button_spot = new JButton(".");
button_spot.addActionListener(numbersAndOperatorsListener(result_TextField));
//加
JButton button_add = new JButton("+");
button_add.addActionListener(numbersAndOperatorsListener(result_TextField));
//减
JButton button_subtract = new JButton("-");
button_subtract.addActionListener(numbersAndOperatorsListener(result_TextField));
//乘
JButton button_multiply = new JButton("*");
button_multiply.addActionListener(numbersAndOperatorsListener(result_TextField));
//除
JButton button_divide = new JButton("/");
button_divide.addActionListener(numbersAndOperatorsListener(result_TextField));
//等于
JButton button_equal = new JButton("=");
button_equal.addActionListener(equalButtonListener(result_TextField));
//创建JPanel面板组件并且采用 BorderLayout布局格式
JPanel panel = new JPanel(new BorderLayout());
//在面板中添加文本框组件-->靠左
panel.add(result_TextField,BorderLayout.WEST);
//在面板中添加文本框组件-->靠右
panel.add(clear_Button, BorderLayout.EAST);
JPanel pan1 = new JPanel(new GridLayout(1,4,5,5));
pan1.add(button7);
pan1.add(button8);
pan1.add(button9);
pan1.add(button_divide);
JPanel pan2 = new JPanel(new GridLayout(1,4,5,5));
pan2.add(button4);
pan2.add(button5);
pan2.add(button6);
pan2.add(button_multiply);
JPanel pan3 = new JPanel(new GridLayout(1,4,5,5));
pan3.add(button1);
pan3.add(button2);
pan3.add(button3);
pan3.add(button_subtract);
JPanel pan4 = new JPanel(new GridLayout(1,4,5,5));
pan4.add(button0);
pan4.add(button_spot);
pan4.add(button_equal);
pan4.add(button_add);
//在容器上添加面板控件
container.add(panel);
container.add(pan1);
container.add(pan2);
container.add(pan3);
container.add(pan4);
//将容器外部特性实例化
setTitle(" Calculator ");
//设窗体的大小 宽和高
setSize(400,250);
//设定窗体的可视化
setVisible(true);
//设置窗体的关闭方式
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//数字和运算符监听事件
public ActionListener numbersAndOperatorsListener(JTextField result_TextField ){
return new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JButton jButton = (JButton) e.getSource();
System.out.println("获取事件监听器:"+jButton.getText());
string += jButton.getText();
result_TextField.setText(string);
}
};
}
//使用JDK自带的类可以实现执行字符串中的运算公式的功能
static ScriptEngine javaScriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
//等于运算
public ActionListener equalButtonListener(JTextField result_TextField){
return new ActionListener() {
String str =null;
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("result_TextField -->"+ result_TextField.getText());
str = result_TextField.getText();
string = equal(str);
result_TextField.setText(string);
}
public String equal(String str){
String result ="";
try {
System.out.println(javaScriptEngine.eval(str));
result = javaScriptEngine.eval(str).toString();
} catch (Exception t) {
}
return result;
}
};
}
//清除文本框的内容
public ActionListener clearButtonListener(JTextField result_TextField){
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
string = "";
result_TextField.setText(string);
}
};
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
|