如何从文本窗格中获取文本?用它做数学运算?(Java)



所以,现在我正试图用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, 0, /, *, -, +, 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, 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, 15));
mathDoerConsole.setForeground(new Color(200, 200, 200));
mathDoerConsole.setFont(new Font("Courier New", Font.PLAIN, 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, mathDoerTextWidth, 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, buttonSize,     ".",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoerNegitiveButton =    initButton(column*1+1,  row*4+108,  buttonSize, buttonSize,     "-/+",      new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer0Button =           initButton(column*2+1,  row*4+108,  buttonSize, buttonSize,     "0",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer1Button =           initButton(column*1+1,  row*3+108,  buttonSize, buttonSize,     "1",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer2Button =           initButton(column*2+1,  row*3+108,  buttonSize, buttonSize,     "2",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer3Button =           initButton(column*3+1,  row*3+108,  buttonSize, buttonSize,     "3",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer4Button =           initButton(column*1+1,  row*2+108,  buttonSize, buttonSize,     "4",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer5Button =           initButton(column*2+1,  row*2+108,  buttonSize, buttonSize,     "5",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer6Button =           initButton(column*3+1,  row*2+108,  buttonSize, buttonSize,     "6",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer7Button =           initButton(column*1+1,  row*1+108,  buttonSize, buttonSize,     "7",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer8Button =           initButton(column*2+1,  row*1+108,  buttonSize, buttonSize,     "8",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoer9Button =           initButton(column*3+1,  row*1+108,  buttonSize, buttonSize,     "9",        new Color(10, 15, 15),  new Color(200, 200, 200),   true);
mathDoerDivideButton =      initButton(column*4+1,  row*0+108,  buttonSize, buttonSize,     " / ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true);
mathDoerMultiplyButton =    initButton(column*4+1,  row*1+108,  buttonSize, buttonSize,     " * ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true);
mathDoerAdditionButton =    initButton(column*4+1,  row*3+108,  buttonSize, buttonSize,     " + ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true);
mathDoerSubtractionButton = initButton(column*4+1,  row*2+108,  buttonSize, buttonSize,     " - ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true); 
mathDoerAlphaButton =       initButton(column*0+1,  row*1+108,  buttonSize, buttonSize,     "α",       new Color(15, 15, 10),  new Color(200, 200, 200),   false);
mathDoerBetaButton =        initButton(column*0+1,  row*2+108,  buttonSize, buttonSize,     "β",       new Color(15, 15, 10),  new Color(200, 200, 200),   false);
mathDoerGammaButton =       initButton(column*0+1,  row*3+108,  buttonSize, buttonSize,     "γ",       new Color(15, 15, 10),  new Color(200, 200, 200),   false);
mathDoerDeltaButton =       initButton(column*0+1,  row*4+108,  buttonSize, buttonSize,     "δ",       new Color(15, 15, 10),  new Color(200, 200, 200),   false);
mathDoerEqualButton =       initButton(column*4+1,  row*4+108,  buttonSize, buttonSize,     " = ",      new Color(10, 15, 10),  new Color(200, 200, 200),   false);
mathDoerClearButton =       initButton(column*0+1,  row*0+108,  buttonSize, buttonSize,     "Clear",    new Color(10, 15, 10),  new Color(200, 200, 200),   false);
mathDoerStartParentheses =  initButton(column*1+1,  row*0+108,  buttonSize, buttonSize,     " ( ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true);
mathDoerEndParentheses =    initButton(column*2+1,  row*0+108,  buttonSize, buttonSize,     " ) ",      new Color(15, 10, 15),  new Color(200, 200, 200),   true);
mathDoerCaretKey =          initButton(column*3+1,  row*0+108,  buttonSize, buttonSize,     " ^ ",      new Color(15, 10, 15),  new Color(200, 200, 200),   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";转换为整数"不是整数,而是整数运算。我建议您正确地将字符串解析为";运算"s"中的整数;以及";操作";然后进行相应的计算。

最新更新