2011-10-18 102 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