如何从 textPane 中获取文本并使用它进行数学运算? (爪哇)

问题描述

所以,现在我正在尝试用 Java 构建一个计算器,我已经构建了 GUI,但是我找不到从窗格中获取文本并对其进行必要数学运算的方法。 例子 文本窗格说

1 + 2

我希望能够按下一个按钮并计算它并进行数学运算并将其显示

1 + 2 = 3

我试过获取文本并使用 parseInt、parseFloat 和 parseDouble。每次我尝试将文本转换为 Int、Double 或 Float 时,都会出现错误错误

“线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“1 + 1””

关于我可以做什么的任何想法

代码

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

//General information: SOL is a 

//Sorry for what I'm sure is sloppy code I'm new to programming

public class MathDoer_Main {

//initializing all of the frames,the text pane,key listeners,the ScrollPane,and all of the buttons
    
    public static JFrame mathDoerFrame = new JFrame("mathDoer");
    public static JTextPane mathDoerText = new JTextPane();
    public static JScrollPane mathDoerTextScoll = new JScrollPane();
    public static JTextPane mathDoerConsole = new JTextPane();
    public static JScrollPane mathDoerConsoleScroll = new JScrollPane();
    public static JButton mathDoer0Button;
    public static JButton mathDoerClearButton = new JButton("Clear");
    public static JButton mathDoerDecimalButton;
    public static JButton mathDoer1Button;
    public static JButton mathDoer2Button;
    public static JButton mathDoer3Button;
    public static JButton mathDoer4Button;
    public static JButton mathDoer5Button;
    public static JButton mathDoer6Button;
    public static JButton mathDoer7Button;
    public static JButton mathDoer8Button;
    public static JButton mathDoer9Button;
    public static JButton mathDoerDivideButton;
    public static JButton mathDoerMultiplyButton;
    public static JButton mathDoerAdditionButton;
    public static JButton mathDoerSubtractionButton;
    public static JButton mathDoerNegitiveButton;
    public static JButton mathDoerFormulaButton;
    public static JButton mathDoerAlphaButton;
    public static JButton mathDoerBetaButton;
    public static JButton mathDoerGammaButton;
    public static JButton mathDoerDeltaButton;
    public static JButton mathDoerThirdButton;
    public static JButton mathDoerEqualButton;
    public static JButton mathDoerStartParentheses;
    public static JButton mathDoerEndParentheses;
    public static JButton mathDoerCaretKey;
    public static JButton mathDoerMainScreen;
    public static MathDoerKeyListener sea_MathDoerKeyListener;

    
    public static void main(String[] args) {
        
    //These integers are used to set the size of the frame,and the textPane
        int mathDoerWidth = 397;
        int mathDoerHeight = 527;
        int mathDoerTextWidth = mathDoerHeight - 1 - 71;
    
    //This keyListener just adds keyBoard Controls for each button (I.E. 1,2,3,4,5,6,7,8,9,/,*,-,+,Clear,Enter)
        sea_MathDoerKeyListener = new MathDoerKeyListener();
        
    //Defining everything to do with the frame,textPane,and scrollPane
        mathDoerFrame.setSize(mathDoerWidth,mathDoerHeight);
        mathDoerFrame.setLocationRelativeto(null);
        mathDoerFrame.setLayout(null);
        mathDoerFrame.setDefaultCloSEOperation(2);
        mathDoerFrame.setResizable(true);
        mathDoerFrame.getContentPane().setBackground(new Color(0,0));
        mathDoerFrame.addKeyListener(sea_MathDoerKeyListener);
        mathDoerFrame.setResizable(false);
        mathDoerFrame.setVisible(true);
    
    //This text is the History
        mathDoerFrame.add(mathDoerText);
        mathDoerText.setBackground(new Color(15,15,15));
        mathDoerText.setForeground(new Color(200,200,200));
        mathDoerText.setFont(new Font("Courier New",Font.PLAIN,12));
        mathDoerText.setEditable(false);
        mathDoerText.addKeyListener(sea_MathDoerKeyListener);
        
        mathDoerTextScoll.setVisible(true);
        mathDoerTextScoll = new JScrollPane();
        mathDoerTextScoll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        mathDoerTextScoll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        mathDoerTextScoll.setBounds(1,1,mathDoerTextWidth,88);
        mathDoerTextScoll.getViewport().add(mathDoerText);
        mathDoerTextScoll.setBorder(null);
        mathDoerFrame.add(mathDoerTextScoll);   
        
    //This text is the one the buttons are editing
        mathDoerFrame.add(mathDoerConsole);
        mathDoerConsole.setBackground(new Color(15,15));
        mathDoerConsole.setForeground(new Color(200,200));
        mathDoerConsole.setFont(new Font("Courier New",12));
        mathDoerConsole.setEditable(true);
        mathDoerConsole.addKeyListener(sea_MathDoerKeyListener);
        
        mathDoerConsoleScroll.setVisible(true);
        mathDoerConsoleScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        mathDoerConsoleScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        mathDoerConsoleScroll.setBounds(1,89,18);
        mathDoerConsoleScroll.getViewport().add(mathDoerConsole);
        mathDoerConsoleScroll.setBorder(null);
        mathDoerFrame.add(mathDoerConsoleScroll);
        
    //Used to make a grid system for the buttons
        int buttonSize = 75;
        int column = buttonSize + 1;
        int row = buttonSize + 1;
        
    //Defining all of the Necessary Things for the button (for the Method(I think that's the right term) see line 184)
        mathDoerDecimalButton =     initButton(column*3+1,row*4+108,buttonSize,".",new Color(10,15),new Color(200,200),true);
        mathDoerNegitiveButton =    initButton(column*1+1,"-/+",true);
        mathDoer0Button =           initButton(column*2+1,"0",true);
        mathDoer1Button =           initButton(column*1+1,row*3+108,"1",true);
        mathDoer2Button =           initButton(column*2+1,"2",true);
        mathDoer3Button =           initButton(column*3+1,"3",true);
        mathDoer4Button =           initButton(column*1+1,row*2+108,"4",true);
        mathDoer5Button =           initButton(column*2+1,"5",true);
        mathDoer6Button =           initButton(column*3+1,"6",true);
        mathDoer7Button =           initButton(column*1+1,row*1+108,"7",true);
        mathDoer8Button =           initButton(column*2+1,"8",true);
        mathDoer9Button =           initButton(column*3+1,"9",true);
        mathDoerDivideButton =      initButton(column*4+1,row*0+108," / ",new Color(15,10,true);
        mathDoerMultiplyButton =    initButton(column*4+1," * ",true);
        mathDoerAdditionButton =    initButton(column*4+1," + ",true);
        mathDoerSubtractionButton = initButton(column*4+1," - ",true); 
        mathDoerAlphaButton =       initButton(column*0+1,"α",10),false);
        mathDoerBetaButton =        initButton(column*0+1,"β",false);
        mathDoerGammaButton =       initButton(column*0+1,"γ",false);
        mathDoerDeltaButton =       initButton(column*0+1,"δ",false);
        mathDoerEqualButton =       initButton(column*4+1," = ",false);
        mathDoerClearButton =       initButton(column*0+1,"Clear",false);
        mathDoerStartParentheses =  initButton(column*1+1," ( ",true);
        mathDoerEndParentheses =    initButton(column*2+1," ) ",true);
        mathDoerCaretKey =          initButton(column*3+1," ^ ",true);
        
    //These the specialized buttons when pressed 
        mathDoerAlphaButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Formula Screen");
            }
        });
        
        mathDoerEqualButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                calculate();
            }
        });
        
        mathDoerClearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mathDoerConsole.setText("");
                System.out.println("clear");
            }
        });
        
    }
    
    public static JButton initButton(int x,int y,int width,int height,String text,Color backgroundColor,Color foregroundColor,boolean defaultAction) {
        JButton button = new JButton(text); 
        mathDoerFrame.add(button);
        button.setBounds(x,y,width,height);
        button.setBackground(backgroundColor);
        button.setForeground(foregroundColor);
        button.setBorderPainted(false);
        button.addKeyListener(sea_MathDoerKeyListener);
        if (defaultAction) {
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println(button.getText());
                    mathDoerConsole.setText(mathDoerConsole.getText() + button.getText());
                }
            });
        }
        return button;
        }

    //The method is the main calculation
    public static void calculate() {
        System.out.println(mathDoerConsole.getText());
        double math = Double.parseDouble(mathDoerConsole.getText());
        System.out.println(Double.toString(math));
    }
    
}

如果您需要更多信息,请随时告诉我,或询问

解决方法

如果我理解正确,您是在尝试将字符串“1+1”转换为整数。 “+”不是整数而是整数运算。我建议您将字符串正确解析为“操作中的整数”和“操作”,然后进行相应的计算。