2011-10-18 3 views
0

누군가 내게 java를 가르쳐 줄 수 있습니까? 아래의 코드는 JOptionPane을 사용하는 것 뿐이며 사용자에게 데이터를 가져 오기위한 입력 대화 상자에 대한 것입니다.JOptionPane 입력 대화 상자

컨셉 : 첫 번째 옵션은 트랜잭션을 선택하는 것입니다. 그런 다음 다른 입력 대화 상자가 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) { 시도 { \t String msg = ""; \t 문자열 myQuery = ""; \t myQuery = "성, 이름 선택"+ \t \t \t "tblPerson WHERE PersonID ="+ selID; \t 명세서 myCommand = con.createStatement(); \t ResultSet selRecord = myCommand.executeQuery (myQuery); \t selRecord.다음 것(); \t msg = "ID"+ ""+ "이름 \ n"; \t msg + = selID + ""+ selRecord.getString (1) + ""+ selRecord.getString (2); \t JOptionPane.showMessageDialog (null, msg); \t selRecord.close(); \t myCommand.close(); } catch (Exception 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