虽然java的图形界界面化技术大部分都已经没有人使用了,但应老师的课堂要求,还是进行了一些简单的学习,并实现了一个图形界面化的可混合运算的简易计算器
这个计算器主要使用了java本身带有的Swing实现图形界面化,以及学习了部分的栈来实现混合运算
先把代码放上,除了(-)按键还没做好之外,其他都没什么问题
当然这也并不是完全由我自己独立完成的,借鉴了很多大佬的代码和思想
c
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class Calculate{
private JFrame frame=new JFrame("计算器");
private JPanel textFrame=new JPanel();
private JPanel keyFrame =new JPanel();
private JLabel tipLable = new JLabel();
private JLabel inLable = new JLabel();
private JLabel outLable = new JLabel();
private StringBuilder inText=new StringBuilder();
private StringBuilder outText=new StringBuilder(" ");
private StringBuilder tipText=new StringBuilder(" ");
public static Stack<Double> dateStack=new Stack<>();
public static Stack<String> operatorStack=new Stack<>();
private String butStr[]={"(-)","C","←","÷","7","8","9","×","4","5","6","-","1","2","3","+","%","0",".","="};
StringBuilder resualt= new StringBuilder();
private JButton[] buttons=new JButton[butStr.length];
public static void main(String[] args) {
new Calculate().CreateCalculate();
}
/**
* @Descripition 创建一个计算器对象
*/
public void CreateCalculate (){
//窗体的基本设置
frame.setSize(450,600);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.add(keyFrame,BorderLayout.CENTER);
frame.add(textFrame,BorderLayout.NORTH);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//文本框部分增加标签,设置布局方式
textFrame.add(tipLable);
textFrame.add(inLable);
textFrame.add(outLable);
textFrame.setLayout(new GridLayout(3,1,0,0));
//提示标签的设置
tipLable.setText(" ");
tipLable.setHorizontalAlignment(SwingConstants.RIGHT);
tipLable.setFont(new Font("宋体",Font.BOLD,20));
tipLable.setBounds(-1,0,450,40);
tipLable.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
tipLable.setSize(450,20);
tipLable.setForeground(Color.RED);
//输入标签的设置
inLable.setText(String.valueOf(inText));
inLable.setHorizontalAlignment(JLabel.RIGHT);
inLable.setFont(new Font("宋体",Font.BOLD,30));
inLable.setBounds(-1,40,450,40);
inLable.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
inLable.setForeground(Color.BLACK);
//输出标签的设置
outLable.setText(String.valueOf(outText));
outLable.setHorizontalAlignment(JLabel.RIGHT);
outLable.setFont(new Font("宋体",Font.BOLD,40));
outLable.setBounds(-1,80,450,40);
outLable.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
outLable.setForeground(Color.BLUE);
//按键框架的布局方式
keyFrame.setLayout(new GridLayout(5,4,4,4));
//添加按钮至案件框架,同时分类绑定监听器
for (int i = 0; i < butStr.length; i++) {
buttons[i]=new JButton(butStr[i]);
buttons[i].setFont(new Font("微软雅黑",Font.BOLD,30));
if(strIsArry(butStr[i],"+,-,×,÷,%")){
buttons[i].addActionListener(new operatorButtonHandler());
}else if(strIsArry(butStr[i],"1,2,3,4,5,6,7,8,9,0,." )){
buttons[i].addActionListener(new numberButtonHandler());
}else if(strIsArry(butStr[i],"C,←,(-),=" )){
buttons[i].addActionListener(new specialButtonHanler());
}
keyFrame.add(buttons[i]);
}
}
/**
* @Description 字符串是否包含对比,str:01,02 search:01
* @Param [str, search]
* @Return boolean
*/
public static boolean strIsArry(String search, String str) {
String[] strs= str.split(",");
for (String s : strs) {
if ((s).equals(search)) {
return true;
}
}
return false;
}
public Double dateHandel(){
resualt.delete(0,resualt.length());
for (int i = 0; i <inText.length(); i++) {
if(strIsArry(String.valueOf(inText.charAt(i)),"+,-,*,/,%")){
resualt.append(" ").append(inText.charAt(i)).append(" ");
}else {
resualt.append(inText.charAt(i));
}
}
String str[]=resualt.toString().split(" ");
/*ArrayList<Object> list=new ArrayList<>();
for (int i = 1; i < str.length; i++) {
if(str[i]==" "){
continue;
} else if(strIsArry(str[i],"+,-,*,/,%")){
list.add(str[i]);
}else{
list.add(Double.valueOf(str[i]));
}
}*/
operatorStack.clear();
dateStack.clear();
for (int i = 0; i < str.length; i++) {
if(strIsArry(str[i],"-,+")){
while ((!operatorStack.isEmpty())&&strIsArry(String.valueOf(operatorStack.peek()),"+,-,*,/,%")){
dateStack.push(calculate());
}
operatorStack.push(str[i]);
}else if(strIsArry(str[i],"%,/,*")){
while((!operatorStack.isEmpty())&&strIsArry(operatorStack.peek(),"/,%,*")){
dateStack.push(calculate());
}
operatorStack.push(str[i]);
}else {
dateStack.push(Double.valueOf(str[i]));
}
}
while(!operatorStack.isEmpty()){
dateStack.push(calculate());
}
return dateStack.peek();
}
public Double calculate(){
Double result =(double)0;
Double beforeNum=dateStack.pop();
Double afterNum=dateStack.pop();
String op=operatorStack.pop();
try {
switch (op) {
case "+" -> result = afterNum + beforeNum;
case "-" -> result = afterNum - beforeNum;
case "*" -> result = afterNum * beforeNum;
case "/" -> result = afterNum / beforeNum;
case "%" -> result = afterNum % beforeNum;
}
}
catch (Exception e){
tipLable.setText("运算出错请重新输入");
}
return result;
}
//数字按键
private class numberButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(buttons[4])){
inText.append("7");
} else if (e.getSource()==buttons[5]) {
inText.append("8");
}else if (e.getSource()==buttons[6]) {
inText.append("9");
}else if (e.getSource()==buttons[8]) {
inText.append("4");
}else if (e.getSource()==buttons[9]) {
inText.append("5");
}else if (e.getSource()==buttons[10]) {
inText.append("6");
}else if (e.getSource()==buttons[12]) {
inText.append("1");
}else if (e.getSource()==buttons[13]) {
inText.append("2");
}else if (e.getSource()==buttons[14]) {
inText.append("3");
}else if (e.getSource()==buttons[17]) {
inText.append("0");
}else if (e.getSource()==buttons[18]) {
inText.append(".");
}
inLable.setText(String.valueOf(inText));
}
}
//运算符按键
private class operatorButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttons[3]){
inText.append("/");
} else if (e.getSource()==buttons[7]) {
inText.append("*");
}else if (e.getSource()==buttons[11]) {
inText.append("-");
}else if (e.getSource()==buttons[15]) {
inText.append("+");
}else if (e.getSource()==buttons[16]) {
inText.append("%");
}
inLable.setText(String.valueOf(inText));
}
}
//行为按键
private class specialButtonHanler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(buttons[1])){
inText= new StringBuilder();
outText=new StringBuilder(" ");
inLable.setText(String.valueOf(inText));
outLable.setText(String.valueOf(outText));
dateStack.clear();
operatorStack.clear();
}else if(e.getSource()==buttons[2]){
if(inText.length()>=1){
inText.deleteCharAt(inText.length()-1);
}
inLable.setText(String.valueOf(inText));
} else if (e.getSource()==buttons[0]) {
inText.append("-");
inLable.setText(String.valueOf(inText));
/*inText.insert(0,"(");
inText.insert(0,"-");
inText.append(")");
inLable.setText(String.valueOf(inText));*/
} else if (e.getSource()==buttons[19]){
outLable.setText(String.valueOf(dateHandel()));
}
}
}
}
标签:java,图形界面,inText,else,buttons,计算器,new,getSource,append
From: https://blog.51cto.com/u_16360983/8520277