JOptionPane输入对话框

时间:2011-10-18 00:39:13

标签: java swing joptionpane

有人可以在java上启发我吗?下面的代码只是在inputdialog框中使用JOptionPane和更多内容来获取用户的数据。

概念: 第一个选项是选择交易然后如果他们按下S另一个输入对话框显示要求输入PIN,然后在PIN之后另一个输入对话框显示4个选项,例如撤销,检查余额,存款和退出。

显示另一个输入对话框的过程是什么,然后返回上一个输入对话框的过程是什么?然后,如果输入错误,则首先验证用户输入以显示消息对话框,然后返回上一个输入对话框,该过程是什么?

import javax.swing.*;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String myOptions = "S = Select Transaction\n"
            + "Q = Quit\n"
            + "Enter your choice";
        String myPin = "Enter your PIN";
        String Y = "Yes";
        String N = "No";
        String value = JOptionPane.showInputDialog(
            null, myOptions, "Computerized Automatic Teller Machine", 1);
        if (value.equals("S")) {
            JOptionPane.showInputDialog(
                null, myPin, "Computerized Automatic Teller Machine", 1);
        } else if (value.equals("Q")) {
            JOptionPane.showMessageDialog(
                null, "Are you sure you want to exit?",
                "Computerized Automatic Teller Machine", 1);
        } else {
            JOptionPane.showMessageDialog(
                null, "Please the correct letter!",
                "Computerized Automatic Teller Machine", 1);
            JOptionPane.showInputDialog(null, myOptions,
                "Computerized Automatic Teller Machine", 1);
        }
    }
}//end of class

2 个答案:

答案 0 :(得分:1)

import javax.swing.*;

public class main {
/**
 * @param args
 */

  public static void main(String[] args) {

    String myOptions="S = Select Transaction\n"+
    "Q = Quit\n"+"Enter your choice";

    String myPin="Enter your PIN";
    String Y = "Yes";
    String N = "No";

    String value = null; // CHANGES START HERE
    boolean access = false;
    while (!access){
         value=JOptionPane.showInputDialog(null,myOptions,"Computerized Automatic Teller Machine",1);

          if (value.equals("S") ){
                String pin = JOptionPane.showInputDialog(null, myPin, "Computerized Automatic Teller Machine", 1);
                if (pin.equals("correctpin")){ // <<------ Here you do correct checks for pin
                    access = true;
                    continue;
                } // if pin
          }// if value

          else if(value.equals("Q") ){
              JOptionPane.showMessageDialog(null, "Are you sure you want to exit?","Computerized Automatic Teller Machine", 1);
          }// elseif vale

          else{
              JOptionPane.showMessageDialog(null, "Please the correct letter!","Computerized Automatic Teller Machine", 1);
              continue; //<--- !S and !Q send to the top of the loop
          }// else
      }// while access
    } // main
}//end of class

好的,所以使用布尔值来检查是否授予了访问权限。 你必须自己检查引脚和

continue;

将返回循环的顶部。如果您需要我进一步澄清,请告诉我。

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / < / p>

回答下面的评论:

public boolean getPersonalInfo(int ...){  // <------------- return a boolean

    boolean result = false;
    while (!result){ // again to keep looping for a valid input
        // Your code here...
        // ... ... ... ... 
        if ("".equals(msg))  // to make sure your query has found something
                             // and all other validation checks
            // Action for failed query
        else{
            result = true;
            // Display msg (showMessageDialog) etc
        }
    }
    return result;
}

然后调用此

if (getPersonalInfo(int)){
    // your code
}

答案 1 :(得分:1)

首先,要为多个对话框添加逻辑,您需要将它们放在while循环中。

boolean ok=false;
while (!ok){
  ... do your dialog boxes

  if (... check your stuff here...) ok=true;
}

其次,请考虑使用包含所有问题的单个对话框。 您可以使用JDialog创建一个。

public static void main(String[] arg){
   JDialog d=new JDialog();
   d.setLayout(new GridLayout(4,2));
   d.add(new JLabel("Quesition 1"));
   JTextField f1=new JTextField();
   d.add(f1);
   ... same for a second question ...
   JButton ok=new JButton("OK");
   d.add(ok);
   ok.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
     if(f1.getText().equals(" ... do your testing here )){ 
       JDialog.this.setVisible(false);
     }
   }});
   d.show();

   String s1=f1.getText();
   ... get your validated values here ...
}

<强>第三:  安全是一个问题吗?您应该考虑使用代码来防止密码被抓取 - 例如JPasswordField中