2017-04-09 82 views
1

如何使JOptionPane顯示錯誤如果用戶在JtextField中輸入了除0-9或+ -/*以外的任何內容,則JOptionPane顯示錯誤給用戶。
我看到了幾種不同的方法可能做到這一點...... 也許是一個documentListener或Inputverifyer。如果用戶輸入不是數字(0-9)或算術運算符(+ - * /)

主類

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

    public void display() { 
     setVisible(true); 
    } 

    P2GUI() { 

     f.setSize(425, 180);//450 width and 525 height 
     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) { 


    if (e.getSource() == evaluate) { 
     String fullString = entryField.getText().trim(); 
     if (fullString.matches("\\d+") || fullString.matches("[-+*/]")) { 
      Convert conversion = new Convert(); 
      resultField.setText(conversion.convert(fullString)); 
      eraseTextField(); 
     }else { 
      JOptionPane.showMessageDialog(null,"Wrong input enter a digit or 
    arithmetic operator"); 
      eraseTextField(); 
     } 

    } 

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

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

    } 
} 

轉換。 Java類

package p2gui; 

import java.util.Stack; 

/** 
* 
* @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> s = new Stack<>(); 

     for (int i = 0; i < postfix.length(); i++) { 
      char c = postfix.charAt(i); 
      if (operator(c)) { 
       String b = s.pop(); 
       String a = s.pop(); 
       s.push("(" + a + c + b + ")"); 
      } else { 
       s.push("" + c); 
      } 
     } 

     return s.pop(); 
    } 

} 
+0

這取決於。你可以使用DocumentFilter進行實時驗證,InputVerifier/ActionListener/FocusListener進行後驗證 – MadProgrammer

回答

0

您可以使用正則表達式來解決你的問題,這裏是一個簡單的例子,你可以用它來解決你的問題:

String str = "123"; 
if (str.matches("\\d+")) { 
    JOptionPane.showMessageDialog(null, "Degit"); 
} else if (str.matches("[-+*/]")) { 
    JOptionPane.showMessageDialog(null, "arithmetic operator(+ - * /)"); 
}else{ 
    JOptionPane.showMessageDialog(null, "Incorrect input"); 
} 

解說

str.matches("[-+*/]")如果您輸入是- + *或/那麼它將返回true。
str.matches("\\d+")如果您的輸入是一個數字,那麼它將返回true

+0

我對(str.matches(「\\ d +」))和(str.matches(「\\ w +」)有點困惑)你能否詳細說明一下。謝謝 –

+0

@MikeR現在檢查我的更新 –

+0

我更新了我的代碼。然而,它不能正常工作... –

0

您可以使用String.matches()來驗證用戶輸入的表達式。

此所以基本代碼:

String expression = "9+45-3/5*9"; //store expression here from text-field. 

boolean isvalid = expression.matches("[0-9-+*/]+"); 

if(isvalid) 
    JOptionPane.showMessageDialog(null,"Valid Expression!"); 
else 
    JOptionPane.showMessageDialog(null,"Not Valid Expression!"); 

還在這裏,你需要檢查是否有兩家運營商不斷地談到其他要求和其他的東西爲好。