Java混合计算器以及界面切换

简介: Java混合计算器以及界面切换

序言: 从这之后博客的更新将会逐渐减少,Java后面的网络编程、Web、JDBC等的学习也要放在一年之后了,接下来的一年将会走考研路;所以就用几天的时间写了一篇混合计算器以及计算器科学型和标准型界面切换(主要包含了JFrame,String类,抛出异常,按钮切换界面),可能存在许多Bug(只做参考)

计算器标准型和科学型界面切换:

package GUI界面切换;
import java.awt.*;
import javax.swing.*;
/***
 * @author  201805050254 AiY
 */
public class View extends JFrame {
    public void view()
    {
        ViewAction exwpAction=new ViewAction(this);//提前定义好动作对象
        setLayout(null);//使用BorderLayout布局
        setBounds(500,450,850,500);//设置界面大小
        this.setTitle("科学型界面");//设置界面名称
        JButton change=new JButton("标准型");//设置切换的按钮eb
        change.setBounds(0,0,80,20);
        add(change);//将按钮eb添加到布局的下方
        JLabel  label1=new JLabel("查看");
        JLabel  label2=new JLabel("语言");
        JLabel  label3=new JLabel("帮助");
        change.addActionListener(exwpAction);
        change.setBounds(0,3,70,20);
        label1.setBounds(90,3,70,20);
        label1.setForeground(Color.RED);
        label2.setBounds(130,3,70,20);
        label2.setForeground(Color.GREEN);
        label3.setBounds(170,3,70,20);
        label3.setForeground(Color.BLUE);
        add(change);
        add(label1);
        add(label2);
        add(label3);
        setVisible(true);//使视图可视化
        JTextField text=new JTextField("0.0");
        text.setBounds(60,30,640,20);
        add(text);
        JRadioButton sixteen=new JRadioButton("十六进制");
        JRadioButton ten=new JRadioButton("十进制");
        JRadioButton eight=new JRadioButton("八进制");
        JRadioButton two=new JRadioButton("二进制");
        JRadioButton angle=new JRadioButton("角度");
        JRadioButton radian=new JRadioButton("弧度");
        JRadioButton gradient=new JRadioButton("梯度");
        //选择框
        sixteen.setBounds(20,60,80,15);
        ten.setBounds(98,60,80,15);
        eight.setBounds(175,60,80,15);
        two.setBounds(260,60,80,15);
        angle.setBounds(340,60,80,15);
        radian.setBounds(420,60,80,15);
        gradient.setBounds(500,60,80,15);
        add(sixteen);add(ten);add(eight);add(two);
        add(angle);add(radian);add(gradient);
        //复选框
        JCheckBox c1 = new JCheckBox("Inv");
        JCheckBox c2 = new JCheckBox("Hyp");
        c1.setBounds(20,100,60,15);
        c2.setBounds(80,100,60,15);
        add(c1);add(c2);
        JButton button0=new JButton("But0");
        JButton button1=new JButton("But1");
        JButton button2=new JButton("But2");
        JButton button3=new JButton("But3");
        JButton button4=new JButton("But4");
        JButton button5=new JButton("But5");
        JButton button6=new JButton("But6");
        JButton button7=new JButton("But7");
        JButton button8=new JButton("But8");
        JButton button9=new JButton("But9");
        JButton button10=new JButton("But10");
        JButton button11=new JButton("But11");
        JButton button12=new JButton("But12");
        JButton button13=new JButton("But13");
        JButton button14=new JButton("But14");
        JButton button15=new JButton("But15");
        JButton button16=new JButton("But16");
        JButton button17=new JButton("But17");
        JButton button18=new JButton("But18");
        JButton button19=new JButton("But19");
        JButton button20=new JButton("But20");
        JButton button21=new JButton("But21");
        JButton button22=new JButton("But22");
        JButton button23=new JButton("But23");
        JButton button24=new JButton("But24");
        button0.setBounds(20,140,70,25);
        button1.setBounds(95,140,70,25);
        button2.setBounds(170,140,70,25);
        button3.setBounds(245,140,70,25);
        button4.setBounds(320,140,70,25);
        button5.setBounds(20,175,70,25);
        button6.setBounds(95,175,70,25);
        button7.setBounds(170,175,70,25);
        button8.setBounds(245,175,70,25);
        button9.setBounds(320,175,70,25);
        button10.setBounds(20,210,70,25);
        button11.setBounds(95,210,70,25);
        button12.setBounds(170,210,70,25);
        button13.setBounds(245,210,70,25);
        button14.setBounds(320,210,70,25);
        button15.setBounds(20,245,70,25);
        button16.setBounds(95,245,70,25);
        button17.setBounds(170,245,70,25);
        button18.setBounds(245,245,70,25);
        button19.setBounds(320,245,70,25);
        button20.setBounds(20,280,70,25);
        button21.setBounds(95,280,70,25);
        button22.setBounds(170,280,70,25);
        button23.setBounds(245,280,70,25);
        button24.setBounds(320,280,70,25);
        add(button0); add(button1); add(button2);add(button3);add(button4);
        add(button5); add(button6); add(button7);add(button8);add(button9);
        add(button10);add(button11);add(button12);add(button13);add(button14);
        add(button15);add(button16);add(button17);add(button18);add(button19);
        add(button20);add(button21);add(button22);add(button23);add(button24);
        JButton clear1=new JButton("清除");
        JButton clear2=new JButton("清除");
        JButton clear3=new JButton("清除");
        clear1.setBounds(700,100,70,25);
        clear2.setBounds(620,100,70,25);
        clear3.setBounds(540,100,70,25);
        add(clear1);add(clear2);add(clear3);
        init();
        JLabel label= new JLabel("AiY 正版授权,盗版必究!!!!");
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        label.setForeground(Color.BLUE);
        label.setBounds(100,350,300,50);
        add(label);
    }
    public void init(){
        JButton button1=new JButton("7");
        JButton button2=new JButton("8");
        JButton button3=new JButton("9");
        JButton button4=new JButton("B3");
        JButton button5=new JButton("B4");
        JButton button6=new JButton("B5");
        JButton button7=new JButton("B6");
        JButton button8=new JButton("B7");
        JButton button9=new JButton("B8");
        JButton button10=new JButton("B9");
        JButton button11=new JButton("B10");
        JButton button12=new JButton("B11");
        JButton button13=new JButton("B12");
        JButton button14=new JButton("B13");
        JButton button15=new JButton("B14");
        JButton button16=new JButton("B15");
        JButton button17=new JButton("B16");
        JButton button18=new JButton("B17");
        JButton button19=new JButton("B18");
        JButton button20=new JButton("B19");
        JButton button21=new JButton("B20");
        JButton button22=new JButton("B21");
        JButton button23=new JButton("B22");
        JButton button24=new JButton("B23");
        JButton button25=new JButton("B24");
        JButton button26=new JButton("B25");
        JButton button27=new JButton("B26");
        JButton button28=new JButton("B27");
        JButton button29=new JButton("B28");
        JButton button30=new JButton("B29");
        add(button1);add(button2);add(button3);add(button4);add(button5);add(button6);
        button1.setBounds(400,140,60,25);
        button2.setBounds(470,140,60,25);
        button3.setBounds(540,140,60,25);
        button4.setBounds(610,140,60,25);
        button5.setBounds(680,140,60,25);
        button6.setBounds(750,140,60,25);
        add(button7);add(button8);add(button9);add(button10);add(button11);add(button12);
        button7.setBounds(400,175,60,25);
        button8.setBounds(470,175,60,25);
        button9.setBounds(540,175,60,25);
        button10.setBounds(610,175,60,25);
        button11.setBounds(680,175,60,25);
        button12.setBounds(750,175,60,25);
        add(button13);add(button14);add(button15);add(button16);add(button17);add(button18);
        button13.setBounds(400,210,60,25);
        button14.setBounds(470,210,60,25);
        button15.setBounds(540,210,60,25);
        button16.setBounds(610,210,60,25);
        button17.setBounds(680,210,60,25);
        button18.setBounds(750,210,60,25);
        add(button19);add(button20);add(button21);add(button22);add(button23);add(button24);
        button19.setBounds(400,245,60,25);
        button20.setBounds(470,245,60,25);
        button21.setBounds(540,245,60,25);
        button22.setBounds(610,245,60,25);
        button23.setBounds(680,245,60,25);
        button24.setBounds(750,245,60,25);
        add(button25);add(button26);add(button27);add(button28);add(button29);add(button30);
        button25.setBounds(400,280,60,25);
        button26.setBounds(470,280,60,25);
        button27.setBounds(540,280,60,25);
        button28.setBounds(610,280,60,25);
        button29.setBounds(680,280,60,25);
        button30.setBounds(750,280,60,25);
    }
    public void view2()
    {
        ViewAction exwpAction=new ViewAction(this);
        setLayout(null);
        setBounds(500,450,600,500);
        this.setTitle("标准型界面");
        JButton change=new JButton("科学型");
        JLabel  label1=new JLabel("查看");
        JLabel  label2=new JLabel("语言");
        JLabel  label3=new JLabel("帮助");
        change.addActionListener(exwpAction);
        change.setBounds(0,3,70,20);
        label1.setBounds(90,3,70,20);
        label1.setForeground(Color.RED);
        label2.setBounds(130,3,70,20);
        label2.setForeground(Color.GREEN);
        label3.setBounds(170,3,70,20);
        label3.setForeground(Color.BLUE);
        add(change);
        add(label1);
        add(label2);
        add(label3);
        JTextField text1=new JTextField();
        JTextField text2=new JTextField();
        JButton buttonBack=new JButton("BackSpace");
        JButton buttonClear=new JButton("Clear");
        JButton buttonLz=new JButton("(");
        JButton buttonRz=new JButton(")");
        JLabel label=new JLabel("AiY正版授权,盗版必究!!!!");
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        label.setForeground(Color.RED);
        label.setBounds(100,400,300,30);
        add(label);
        //输入框
        add(text1).setBounds(80,25,450,30);
        //计算框
        add(text2).setBounds(80,65,450,30);
        //按钮BackSpace
        buttonBack.setBounds(50,100,100,30);add(buttonBack);
        //按钮Clear
        buttonClear.setBounds(180,100,100,30);add(buttonClear);
        //按钮(
        buttonLz.setBounds(310,100,100,30);add(buttonLz);
        //按钮 )
        buttonRz.setBounds(440,100,100,30);add(buttonRz);
        JButton button1=new JButton("7");
        JButton button2=new JButton("8");
        JButton button3=new JButton("9");
        JButton button4=new JButton("/");
        JButton button5=new JButton("4");
        JButton button6=new JButton("5");
        JButton button7=new JButton("6");
        JButton button8=new JButton("*");
        JButton button9=new JButton("1");
        JButton button10=new JButton("2");
        JButton button11=new JButton("3");
        JButton button12=new JButton("-");
        JButton button13=new JButton("0");
        JButton button14=new JButton(".");
        JButton button15=new JButton("=");
        JButton button16=new JButton("+");
        //按钮7
        button1.setBounds(50,155,100,30);add(button1);
        //按钮8
        button2.setBounds(180,155,100,30);add(button2);
        //按钮9
        button3.setBounds(310,155,100,30);add(button3);
        //按钮/
        button4.setBounds(440,155,100,30);add(button4);
        //按钮4
        button5.setBounds(50,210,100,30);add(button5);
        //按钮5
        button6.setBounds(180,210,100,30);add(button6);
        //按钮6
        button7.setBounds(310,210,100,30);add(button7);
        //按钮*
        button8.setBounds(440,210,100,30);add(button8);
        //按钮1
        button9.setBounds(50,265,100,30);add(button9);
        //按钮2
        button10.setBounds(180,265,100,30);add(button10);
        //按钮3
        button11.setBounds(310,265,100,30);add(button11);
        //按钮-
        button12.setBounds(440,265,100,30);add(button12);
        //按钮0
        button13.setBounds(50,320,100,30);add(button13);
        //按钮 .
        button14.setBounds(180,320,100,30);add(button14);
        //按钮 =
        button15.setBounds(310,320,100,30);add(button15);
        //按钮+
        button16.setBounds(440,320,100,30);add(button16);
        setVisible(true);
    }
    public static void main(String[] args) {
        View v=new View();
        v.view2();
    }
}


/***
 * @author  201805050254 AiY
 */
//事件监听
package GUI界面切换;
import java.awt.event.ActionEvent;
public class ViewAction implements java.awt.event.ActionListener {
    View v=new View();
    public ViewAction(View v)
    {
        this.v=v;
    }
    public void actionPerformed(ActionEvent event)
    {
        Object object=event.getSource();//创建事件源对象
        switch(event.getActionCommand()){
            //如果按下视图1
            case "科学型":
                new View().view();//new一个View类并调用里面的view函数
                v.setVisible(false);//关闭穿进来的那个类的视图
                break;
            //如果按下视图2
            case "标准型":
                new View().view2();
                v.setVisible(false);
                break;
        }
    }
}



标准型界面:

科学型界面:

混合计算器的简单计算:

package 混合计算器;
/***
 * @author  201805050254 AiY
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

public class MyComputer {
    public static void main(String[] args) {
        JieMia Computer = new JieMia("我的Computer");
    }
}
class JieMia extends JFrame {
    //text1文本框中的字符串
    String s=null;
    //算术运算的规则就是小数点、等号只能出现一次,用变量记录
    int point=0,equal=0;
    //运算法则中有括号先算括号里面的,用变量记录括号出现的次数
    int leftCount=0,rightCount=0;
    JieMia(String title){
        super(title);
        setLayout(null);
        JTextField text1=new JTextField();
        JTextField text2=new JTextField();
        JButton buttonBack=new JButton("BackSpace");
        JButton buttonClear=new JButton("Clear");
        JButton buttonLz=new JButton("(");
        JButton buttonRz=new JButton(")");
        JLabel label=new JLabel("AiY正版授权,违者必究!!!!");
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        label.setForeground(Color.RED);
        label.setBounds(100,400,300,30);
        add(label);
        //输入框
       add(text1).setBounds(80,10,450,30);
       //计算框
       add(text2).setBounds(80,45,450,30);
       //按钮BackSpace
        buttonBack.setBounds(50,100,100,30);add(buttonBack);
        buttonBack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>0){
                    text1.setText(s.substring(0,s.length()-1));
                }
            }
        });
        //按钮Clear
        buttonClear.setBounds(180,100,100,30);add(buttonClear);
        buttonClear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                text2.setText("");
                text1.setText("0");
                point=0;equal=0;
                leftCount=0;rightCount=0;
            }
        });
        //按钮(
        buttonLz.setBounds(310,100,100,30);add(buttonLz);
        buttonLz.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(!text1.getText().equals("0")&&!text1.getText().contentEquals("")){
                    text2.setText(text2.getText()+text1.getText());
                }
                s=text2.getText();
                char char1[]=s.toCharArray();
                int length=s.length()-1;
                if(length==-1||char1[length]=='+'||char1[length]=='-'||char1[length]=='*'||char1[length]=='/'||char1[length]=='('||s.equals("")){
                    text2.setText(text2.getText()+'(');
                    leftCount++;
                }
            }
        });
        //按钮 )
        buttonRz.setBounds(440,100,100,30);add(buttonRz);
        buttonRz.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if(!text1.getText().equals("0")){
                    text2.setText(text2.getText()+text1.getText());
                    text1.setText("");
                }
                s= text2.getText();
                char char1[]=s.toCharArray();
                int length=s.length()-1;
                if((Character.isDigit(char1[length])&&leftCount>rightCount)||((char1[length]==')')&&leftCount>rightCount)){
                    rightCount++;
                    text2.setText(text2.getText()+')');
                }
            }
        });
        /***
         * 设置按钮
         * 7 8 9 /
         * 4 5 6 *
         * 1 2 3 -
         * 0 . = +
         */
        JButton button1=new JButton("7");
        JButton button2=new JButton("8");
        JButton button3=new JButton("9");
        JButton button4=new JButton("/");
        JButton button5=new JButton("4");
        JButton button6=new JButton("5");
        JButton button7=new JButton("6");
        JButton button8=new JButton("*");
        JButton button9=new JButton("1");
        JButton button10=new JButton("2");
        JButton button11=new JButton("3");
        JButton button12=new JButton("-");
        JButton button13=new JButton("0");
        JButton button14=new JButton(".");
        JButton button15=new JButton("=");
        JButton button16=new JButton("+");
        //按钮7
        button1.setBounds(50,155,100,30);add(button1);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("7");
                }else {
                    text1.setText(s+"7");
                }
            }
        });
        //按钮8
        button2.setBounds(180,155,100,30);add(button2);
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("8");
                }else {
                    text1.setText(s+"8");
                }
            }
        });
        //按钮9
        button3.setBounds(310,155,100,30);add(button3);
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("9");
                }else {
                    text1.setText(s+"9");
                }
            }
        });
        //按钮/
        button4.setBounds(440,155,100,30);add(button4);
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                char char1[]=s.toCharArray();
                int length1=s.length()-1;
                String express=text2.getText();
                char char2[]=express.toCharArray();
                int length2=express.length()-1;
                if((length2==-1||char2[length2]!=')')&&(s.equals("0")||s.equals("")||char1[length1]=='.')){

                } else {
                    text1.setText("");
                    text2.setText(text2.getText()+s+"/");
                }
            }
        });
        //按钮4
        button5.setBounds(50,210,100,30);add(button5);
        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("4");
                }else {
                    text1.setText(s+"4");
                }
            }
        });
        //按钮5
        button6.setBounds(180,210,100,30);add(button6);
        button6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("5");
                }else {
                    text1.setText(s+"5");
                }
            }
        });
        //按钮6
        button7.setBounds(310,210,100,30);add(button7);
        button7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("6");
                }else {
                    text1.setText(s+"6");
                }
            }
        });
        //按钮*
        button8.setBounds(440,210,100,30);add(button8);
        button8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                char char1[]=s.toCharArray();
                int length1=s.length()-1;
                String express=text2.getText();
                char char2[]=express.toCharArray();
                int length2=express.length()-1;
                if((length2==-1||char2[length2]!=')')&&(s.equals("0")||s.equals("")||char1[length1]=='.')){

                } else {
                    text1.setText("");
                    text2.setText(text2.getText()+s+"*");
                }
            }
        });
        //按钮1
        button9.setBounds(50,265,100,30);add(button9);
        button9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("1");
                }else {
                    text1.setText(s+"1");
                }

            }
        });
        //按钮2
        button10.setBounds(180,265,100,30);add(button10);
        button10.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("2");
                }else {
                    text1.setText(s+"2");
                }
            }
        });
        //按钮3
        button11.setBounds(310,265,100,30);add(button11);
        button11.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }else if(s.equals("0")||s.equals("")){
                    text1.setText("3");
                }else {
                    text1.setText(s+"3");
                }
            }
        });
        //按钮-
        button12.setBounds(440,265,100,30);add(button12);
        button12.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                char char1[]=s.toCharArray();
                int length1=s.length()-1;
                String express=text2.getText();
                char char2[]=express.toCharArray();
                int length2=express.length()-1;
                if((length2==-1||char2[length2]!=')')&&(s.equals("0")||s.equals("")||char1[length1]=='.')){

                } else {
                    text1.setText("");
                    text2.setText(text2.getText()+s+"-");
                }
            }
        });
        //按钮0
        button13.setBounds(50,320,100,30);add(button13);
        button13.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(equal==1||s.length()>8){

                }else{
                    text1.setText(s+"0");
                }

            }
        });
        //按钮 .
        button14.setBounds(180,320,100,30);add(button14);
        button14.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(s.length()>8||equal==1){

                }
                else if(point==0){
                    point=1;
                    text1.setText(s+".");
                }
            }
        });
        //按钮 =
        button15.setBounds(310,320,100,30);add(button15);
        button15.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                if(!s.equals("0")&&!s.equals("")){
                    text2.setText(text2.getText()+s);
                }
                if(equal==0){
                    text1.setText("");
                    //补全右括号
                    for(int i=0;i<leftCount-rightCount;i++){
                        text2.setText(text2.getText()+')');
                    }
                    //声明栈,将中缀表达式转化为逆波兰表达式
                    String[] anyString=new String[500];
                    int size=0;
                    //定义一个栈表
                    Stack<Character> Operate=new Stack<Character>();
                    s=text2.getText();
                    char char1[] = s.toCharArray();
                    int length=char1.length;
                    for(int i=0;i<length;i++){
                        //当数组元素为0-9
                        if(char1[i]>='0'&&char1[i]<='9'){
                            double number=char1[i]-'0';
                            //一直找,直到不是数字和小数点
                            int point=0; //记录小数点出现次数
                            int bit=1;  //记录小数点出现的位置
                            if(i==length-1){
                                anyString[size++]=String.valueOf(number);
                            }
                            for(i=i+1;i<length;i++){
                                if((char1[i]<'0'||char1[i]>'9')&&char1[i]!='.'){
                                    i--;
                                    anyString[size++]=String.valueOf(number);
                                    break;
                                }
                                if(char1[i]=='.'){
                                    point=1;
                                    continue;
                                }
                                if(char1[i]>='0'&&char1[i]<='9'){
                                    if(point==0){
                                        number=number*10+(char1[i]-'0');
                                    } else{
                                        number=number+Math.pow(10,-bit)*(char1[i]-'0');
                                        bit++;
                                    }
                                }
                            }
                        }else{
                            if(char1[i]=='('){
                                Operate.push('(');
                            }else if(char1[i]==')'){
                                while(!Operate.peek().equals('(')){
                                    anyString[size++]=String.valueOf(Operate.peek());
                                    Operate.pop();
                                }
                                Operate.pop();
                            }else{
                                if(Operate.empty()){
                                    Operate.push(char1[i]);
                                }else {
                                    while(true){
                                        if(Operate.empty()){
                                            Operate.push(char1[i]);
                                            break;
                                        }
                                        char operate1=Operate.peek();
                                        if((char1[i]=='*'||char1[i]=='/')&&(operate1=='+'||operate1=='-')||(operate1=='('||operate1==')')){
                                            Operate.push(char1[i]);
                                            break;
                                        }else {
                                            anyString[size++]=String.valueOf(Operate.peek());
                                            Operate.pop();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    while (!Operate.empty()){
                        anyString[size++]=String.valueOf(Operate.peek());
                        Operate.pop();
                    }
                    Stack<Double>Last=new Stack<Double>();
                    for(int j=0;j<size;j++){
                        String s1=anyString[j];
                        char char2[]=s1.toCharArray();
                        if(char2[0]>='0'&&char2[0]<='9'){
                            Last.push(Double.parseDouble(s1));
                        }else{
                            double num1=Last.pop();
                            double num2=Last.pop();
                            double num3=0;
                            if(char2[0]=='+')
                                num3=num1+num2;
                            else if(char2[0]=='-')
                                num3=num2-num1;
                            else if(char2[0]=='*')
                                num3=num2*num1;
                            else if(char2[0]=='/')
                                num3=num2/num1;
                            Last.push(num3);
                        }
                    }
                    text2.setText(text2.getText()+"="+Last.pop());
                    equal=1;
                }
            }
        });
        //按钮+
        button16.setBounds(440,320,100,30);add(button16);
        button16.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                s=text1.getText();
                // 将表达式转换为字符数组
                char char1[]=s.toCharArray();
                int length1=s.length()-1;
                String express=text2.getText();
                char char2[]=express.toCharArray();
                int length2=express.length()-1;
                if((length2==-1||char2[length2]!=')')&&(s.equals("0")||s.equals("")||char1[length1]=='.')){

                } else {
                    text1.setText("");
                    text2.setText(text2.getText()+s+"+");
                }
            }
        });
        setVisible(true);
        setBounds(300,300,600,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

相关文章
|
6月前
|
Java API 容器
java的图形化界面编程AWT与Swing学习记录与分享(其一container容器)
java的图形化界面编程AWT与Swing学习记录与分享(其一container容器)
108 0
|
Java
Java实现计算器demo
Java实现计算器demo
76 1
|
6月前
|
Java 容器
idea中关于java的图形化界面编程awt_container容器中Button(按钮)上汉字是乱码或者小方框的解决方法
idea中关于java的图形化界面编程awt_container容器中Button(按钮)上汉字是乱码或者小方框的解决方法
130 0
|
23天前
|
开发框架 JavaScript 前端开发
HarmonyOS UI开发:掌握ArkUI(包括Java UI和JS UI)进行界面开发
【10月更文挑战第22天】随着科技发展,操作系统呈现多元化趋势。华为推出的HarmonyOS以其全场景、多设备特性备受关注。本文介绍HarmonyOS的UI开发框架ArkUI,探讨Java UI和JS UI两种开发方式。Java UI适合复杂界面开发,性能较高;JS UI适合快速开发简单界面,跨平台性好。掌握ArkUI可高效打造符合用户需求的界面。
75 8
|
5月前
|
Java 数据安全/隐私保护
利用Java图形化界面组件Swing写一个简易的登录界面
利用Java图形化界面组件Swing写一个简易的登录界面
70 1
|
5月前
|
Java
逆波兰计算器(java)
逆波兰计算器(java)
|
存储 Java
Java设计一个简单的计算器程序
Java设计一个简单的计算器程序
|
6月前
|
Java
Java实现简易计算器
Java实现简易计算器
415 5
|
6月前
|
Java
简单的 Java 计算器示例
【5月更文挑战第1天】这是一个Java计算器程序,接收用户输入的两个数字和一个运算符(+,-,*,/),通过`Scanner`获取输入,使用`switch`进行计算。当运算符为除法时,检查除数是否为0,防止除零错误。最后,它打印出计算结果。
84 0
|
6月前
|
数据可视化 小程序 Java
【Java】——实现可视化加减乘除计算器
【Java】——实现可视化加减乘除计算器
【Java】——实现可视化加减乘除计算器