通过JTextField过滤用户输入的方法?

时间:2017-04-10 06:39:42

标签: java exception user-input jtextfield

我的问题是,如果用户输入除0-9和+ - * / (算术运算符)之外的任何内容。我想要一个JOptionPane弹出并显示消息说明"请输入一个包含字符0-9和+ - * /的Postfix表达式。"

Ex: 11+的后缀是(1 + 1)。现在,id用户输入了11%或ab +我想要弹出JOptionPane。

最好的方法是什么?

主要

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;


    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");//creating instance of JButton  
        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("[-+*/]")){
            JOptionPane.showMessageDialog(null, "Please Enter Digit and 
        Arithmetic operator");
        } else {
            Convert conversion = new Convert();
            resultField.setText(conversion.convert(fullString));
            //eraseTextField();

        }

        }

        public void eraseTextField() {
        entryField.setText("");
        }


        public static void main(String[] args) {
        P2GUI p1GUI;
        p1GUI = new P2GUI();

        }

      }

Convert.java

package p2gui;

 import java.util.Stack;
 import javax.swing.JOptionPane;



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();
}

}

0 个答案:

没有答案