2011-10-18 101 views
0

有人可以启发我在Java?下面的代码只是使用JOptionPane和更多的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 

回答

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; 

将返回到循环的顶部。让我知道你是否需要我进一步澄清。

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

在回答下面的评论:

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 
} 
+0

嗨。感谢您的答复。我想我理解你的代码。我会尝试它,并会让你知道:) – keyframer

+0

嗨,它的作品。我的下一个麻烦是PIN应该从数据库中看。我如何在数据库的表格中插入找到它的方法。以下是我从数据库中读取信息的其他课程。 – keyframer

+0

public void getPersonInfo(int selID){ try { \t String msg =“”; \t String myQuery =“”; \t myQuery =“SELECT LastName,FirstName FROM”+ \t \t \t“tblPerson WHERE PersonID =”+ selID; \t Statement myCommand = con.createStatement(); \t ResultSet selRecord = myCommand.executeQuery(myQuery); \t selRecord。下一个(); \t msg =“ID”+“”+“Name \ n”; \t msg + = selID +“”+ selRecord.getString(1)+“”+ selRecord.getString(2); \t JOptionPane.showMessageDialog(null,msg); \t selRecord.close(); \t myCommand.close(); (例外e){ \t JOptionPane.showMessageDialog(null,e); \t \t} \t} \t } – keyframer

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