我的Action Listener中的我的If Else
声明无法正常工作。如果其他人不工作?
当按下J按钮时,如果用户输入了一个字符串,其数字为0-9
和+-*/
,则程序正常执行。
否则,JOptionPane会显示一条错误消息。
在下面的代码中,它似乎跳过If
的条件,并直接去Else
无论是什么?
如果决定编译该代码..
例后缀:11+就等于(1 + 1)时转化为缀
主
package p2gui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.JOptionPane;
/**
*
* @author Mike
*/
public class P2GUI extends JFrame implements ActionListener {
JFrame f = new JFrame("Three Address Generator");// Title
private final JButton evaluate;
private final JLabel textfieldLabel;
private final JTextField entryField;
private final JLabel resutfieldlabel;
private final JTextField resultField;
private final JOptionPane popup = new JOptionPane();
P2GUI() {
f.setSize(425, 180);
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible //window size
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textfieldLabel = new JLabel("Enter Postfix Expression");
f.add(textfieldLabel);
textfieldLabel.setBounds(10, 10, 160, 25);
entryField = new JTextField("");
//entryField.addActionListener(this);//ActionListener
f.add(entryField);
entryField.setBounds(160, 10, 220, 25);
evaluate = new JButton("Construct Tree");
evaluate.addActionListener(this);//ActionListener
f.add(evaluate);
evaluate.setBounds(137, 55, 130, 30);
resutfieldlabel = new JLabel(" Infix Expression ");
f.add(resutfieldlabel);
resutfieldlabel.setBounds(20, 100, 100, 25);
resultField = new JTextField("");
//resultField.addActionListener(this);//ActionListener
resultField.setEditable(false);
f.add(resultField);
resultField.setBounds(125, 100, 220, 25);
}
@Override
public void actionPerformed(ActionEvent e) {
String fullString;
fullString = entryField.getText().trim();
if(fullString.matches("\\d+") && fullString.matches("[-+*/]")){
Convert conversion = new Convert();
resultField.setText(conversion.convert(fullString));
} else {
JOptionPane.showMessageDialog(null, "Please Enter Digit and
Arithmetic operator");
//eraseTextField();
}
}
public void eraseTextField() {
entryField.setText("");
entryField.requestFocus();
}
public static void main(String[] args) {
P2GUI p1GUI;
p1GUI = new P2GUI();
}
}
/////////////////////////////////////////////END///////////////////////////////////////////////////////////////////////////////
隐蔽类
package p2gui;
import java.util.Stack;
import javax.swing.JOptionPane;
/**
*
* @author Mike
*/
public class Convert {
/**
* Checks if the input is operator or not
* @param c input to be checked
* @return true if operator
*/
private boolean operator(char c){
return c == '+' || c == '-' || c == '*' || c =='/' || c == '^';
}
/**
* Converts any postfix to infix
* @param postfix String expression to be converted
* @return String infix expression produced
*/
public String convert(String postfix){
Stack<String> stackIt = new Stack<>();
for (int i = 0; i < postfix.length(); i++) {
char c = postfix.charAt(i);
if (operator(c)) {
String b = stackIt.pop();
String a = stackIt.pop();
stackIt.push("(" + a + c + b + ")");
} else {
stackIt.push("" + c);
}
}
return stackIt.pop();
}
}
请注意,一个变量只能包含一个值,这是OR和AND运算符中的缺陷 – abcOfJavaAndCPP
@abcOfJava:否,OP正在尝试查明字符串是否包含数字*,并且*包含运算符。 –