实验一
package com.company;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
import javax.swing.*;
//Calculator类,继承JFrame框架,实现事件监听器接口
class Calculator extends JFrame implements ActionListener {
private final String[] KEYS = { "7", "8", "9", "C", "4", "5", "6", "+", "1", "2", "3", "-", "0", "*", "%", "/", "(", ")", ".", "=" };
private JButton keys[] = new JButton[KEYS.length];
private JTextArea resultText = new JTextArea("0");// 文本域组件TextArea可容纳多行文本;文本框内容初始值设为0.0
private JTextArea History = new JTextArea();// 历史记录文本框初始值设为空
private JPanel jp2=new JPanel();
private JScrollPane gdt1=new JScrollPane(resultText);//给输入显示屏文本域新建一个垂直滚动滑条
private JScrollPane gdt2=new JScrollPane(History);//给历史记录文本域新建一个垂直滚动滑条
// private JScrollPane gdt3=new JScrollPane(History);//给历史记录文本域新建一个水平滚动滑条
private JLabel label = new JLabel("历史记录");
private String b = "";
int flash=0;
int flash2=0;
int flash3=0;
// 构造方法
public Calculator() {
super("简易计算器");//“超”关键字,表示调用父类的构造函数,
resultText.setBounds(20, 18, 255, 115);// 设置文本框大小
resultText.setAlignmentX(RIGHT_ALIGNMENT);// 文本框内容右对齐
resultText.setEditable(false);// 文本框不允许修改结果
History.setBounds(250, 40, 200,200);// 设置文本框大小
History.setAlignmentX(LEFT_ALIGNMENT);// 文本框内容右对齐
History.setEditable(false);// 文本框不允许修改结果
label.setBounds(300, 15, 100, 20);//设置标签位置及大小
jp2.setBounds(290,50,250,200);//设置面板窗口位置及大小
jp2.setLayout(new GridLayout());
JPanel jp1 = new JPanel();
jp1.setBounds(20,18,255,120);//设置面板窗口位置及大小
jp1.setLayout(new GridLayout());
resultText.setLineWrap(true);// 激活自动换行功能
resultText.setWrapStyleWord(true);// 激活断行不断字功能
resultText.setSelectedTextColor(Color.RED);
History.setLineWrap(true);//自动换行
History.setWrapStyleWord(true);
History.setSelectedTextColor(Color.blue);
gdt1.setViewportView(resultText);//使滚动条显示出来
gdt2.setViewportView(History);
gdt1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//设置让垂直滚动条一直显示
gdt2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//设置让垂直滚动条一直显示
gdt2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//设置让水平滚动条一直显示
jp1.add(gdt1);//将滚动条添加入面板窗口中
jp2.add(gdt2);
this.add(jp1);//将面板添加到总窗体中
this.add(jp2);//将面板添加到总窗体中
this.setLayout(null);
this.add(label);// 新建“历史记录”标签
//this.add(resultText);// 新建文本框,该语句会添加进去一个新的JTextArea导致带有滚动条的文本无法显示或者发生覆盖
//this.add(History);// 新建历史记录文本框,该语句会添加进去一个新的JTextArea导致带有滚动条的文本无法显示
// 放置按钮
int x = 20, y = 150;
for (int i = 0; i < KEYS.length; i++)
{
keys[i] = new JButton();
keys[i].setText(KEYS[i]);
keys[i].setBounds(x, y, 50, 50);
if (x < 215) {
x += 65;
} else {
x = 20;
y += 53;
}
this.add(keys[i]);
}
for (int i = 0; i < KEYS.length; i++)// 每个按钮都注册事件监听器
{
keys[i].addActionListener(this);
}
this.setResizable(false);
this.setBounds(500, 200, 600, 480);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
// 事件处理
public void actionPerformed(ActionEvent e)
{
//History.setText(b);//使输入的表达式显示在历史记录文本框中
String label=e.getActionCommand();//获得事件源的标签int flash=1;
if(Objects.equals(label, "="))//
{
resultText.setText(this.b);
History.setText(History.getText()+resultText.getText());
if(label.equals("="))//调用计算方法,得出最终结果
{
String[] s =houzhui(this.b);
String result=Result(s);
this.b=result+"";
//更新文本框,当前结果在字符串b中,并未删除,下一次输入接着此结果以实现连续运算
resultText.setText(this.b);
History.setText(History.getText()+"="+resultText.getText()+"\n");
}
}
else if(Objects.equals(label, "C"))//清空按钮,消除显示屏文本框前面所有的输入和结果
{
this.b="";
resultText.setText("0");//更新文本域的显示,显示初始值;
flash=0;
}
else if(label.equals("+")){
if(b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='('&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='%'){
this.b = this.b + label;
resultText.setText(this.b);
flash=1;
}
}
else if(label.equals("-")){
if(b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='%') {
this.b = this.b + label;
resultText.setText(this.b);
flash=1;
}
}else if(label.equals("*")){
if(b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='('&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='%') {
this.b = this.b + label;
resultText.setText(this.b);
flash=1;
}
}else if(label.equals("/")){
if(b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='('&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='%') {
this.b = this.b + label;
resultText.setText(this.b);
flash=1;
}
}
else if(label.equals("%")){
if(b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='('&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='%'){
this.b=this.b+label;
resultText.setText(this.b);
flash=1;
}
}
else if(label.equals(".")){
if (flash==0){
this.b=this.b+label;
resultText.setText(this.b);
flash=1;
}
}
else if(label.equals("(")) {
flash2++;
if (b.charAt(b.length()-1)!=')'&&b.charAt(b.length()-1)!='1'&&b.charAt(b.length()-1)!='2'&&b.charAt(b.length()-1)!='3'&&b.charAt(b.length()-1)!='4'&&b.charAt(b.length()-1)!='5'&&b.charAt(b.length()-1)!='6'&&b.charAt(b.length()-1)!='7'&&b.charAt(b.length()-1)!='8'&&b.charAt(b.length()-1)!='9'&&b.charAt(b.length()-1)!='0'&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!=')'&&b.charAt(b.length()-1)!='%') {
this.b = this.b + label;
resultText.setText(this.b);
}
}
else if(label.equals(")")) {
flash3++;
if (b.charAt(b.length()-1)!=' '&&b.charAt(b.length()-1)!='.'&&b.charAt(b.length()-1)!='-'&&b.charAt(b.length()-1)!='*'&&b.charAt(b.length()-1)!='/'&&b.charAt(b.length()-1)!='('&&b.charAt(b.length()-1)!='+'&&b.charAt(b.length()-1)!='('&&flash3<=flash2) {
this.b = this.b + label;
resultText.setText(this.b);
}
}
else
{ if(label.equals("+")||label.equals("-")||label.equals("*")||label.equals("/"))flash=0;
this.b=this.b+label;
resultText.setText(this.b);
// History.setText(History.getText()+this.b);
}
//History.setText(History.getText()+this.b);//使输入的表达式显示在历史记录文本框中
}
//将中缀表达式转换为后缀表达式
private String[] houzhui(String str) {
String s = "";// 用于承接多位数的字符串
char[] opStack = new char[100];// 静态栈,对用户输入的操作符进行处理,用于存储运算符
String[] postQueue = new St