JOptionPane 输入对话框

发布于 2024-12-10 14:14:18 字数 1414 浏览 0 评论 0原文

有人可以教我java吗?下面的代码只是在输入对话框上使用 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

Can someone enlighten me on java? The code below is just using JOptionPane and more on inputdialog box for getting data on the user.

Concept:
First option is to select transaction then if they press S another input dialog shows asking for a PIN and then after the PIN another input dialog shows with 4 options for example withdrawing,checking balance, depositing and exit.

What is the process of showing another input dialog and then what is the process of going back to previous input dialog? And then what is the process of validating user input to show a message dialog first if the input is wrong and then go back to the previous input dialog?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-12-17 14:14:18
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
}
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

Ok so used a boolean to check if access is granted.
You will have to put in you own checks for the pin and the

continue;

will return to the top of the loop. Let me know if you need me to clarify further.

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

In answer to your comment below :

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;
}

then to call this

if (getPersonalInfo(int)){
    // your code
}
ぶ宁プ宁ぶ 2024-12-17 14:14:18

首先,要向多个对话框添加逻辑,您需要将它们放入 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

Firstly, to add logic to your multiple dialog boxes, you will need to put them inside a while loop.

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

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

Secondly, consider using a single dialog with all the questions in.
You can create one by using 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 ...
}

Thirdly:
Is security an issue here? You should consider using code to prevent passwords from being grabbed - e.g. JPasswordField

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文