【问题标题】:Multiple JOptionPane input dialogs?多个 JOptionPane 输入对话框?
【发布时间】:2017-01-27 22:55:57
【问题描述】:

过去一个小时我一直在寻找,但我一直无法找到我正在寻找的解决方案。

我想使用JOptionPane 从用户那里获取多个输入,但我不希望它们都在一个对话框窗口中。我希望它过渡到下一个,或者只是弹出下一个。有没有办法使用JOptionPane 做到这一点?

这是我目前所拥有的:

import java.util.Scanner;
import javax.swing.*;
public class HomeWork2 {


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);
        Scanner input3 = new Scanner(System.in);
        Scanner input4 = new Scanner(System.in);
        int days, assignments;
        double temperature;
        boolean isRaining;

        JOptionPane.showInputDialog("How many days are left?");
        days = input.nextInt();

        JOptionPane.showInputDialog("How many assignments are due?");
        assignments = input2.nextInt();

        JOptionPane.showInputDialog("What is the temperature outside?");
        temperature = input3.nextDouble();

        JOptionPane.showInputDialog("Is it raining today?");
        isRaining = input4.nextBoolean();

        if(assignments<=0)
            JOptionPane.showMessageDialog(null, "Why are you asking in the first place?");
        else
            if(days<5)
                JOptionPane.showMessageDialog(null, "You need to hurry up, time is short.");
            else
                if(assignments>4)
                    JOptionPane.showMessageDialog(null, "You need to hurry up before the assignments pile up. Oh wait...");
                else
                    if(temperature<50)
                        JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");
                    else
                        if(isRaining==true)
                            JOptionPane.showMessageDialog(null, "It's raining, you might as well start on your assignments.");
                        else
                            JOptionPane.showMessageDialog(null, "It's nice out and you have some time to spare, go have fun.");

        input.close();
        input2.close();
        input3.close();
        input4.close();

    }

}

【问题讨论】:

  • 在编码论坛上提问之前,你应该知道java和js的区别
  • 对不起...它在推荐之下。就像我说的,我只写了几个星期的代码。
  • 1) 您正在混合 2 种范式,控制台应用程序和 GUI 应用程序,如果您已经编写了几个星期的代码,我建议您首先学习控制台应用程序的基础知识。 2)例如Scanner input = new Scanner(System.in);你应该只有其中一个,然后你可以像days = input.nextInt();assignments = input.nextInt();一样做。 3) 要在基于 GUI 的应用程序中获取用户输入,您可以使用 JOptionPane#showInputDialog...
  • ... 4) 你的程序不过是一个main 方法,学会使用方法和参数。 5)JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");,第一个参数不应为空,而是对您的JFrame 的引用。 6)你的缩进是错误的,elif应该都在同一级别,建议使用花括号({}),它们会为你节省很多未来的问题
  • 7) 看来您可能只想在一个窗口中获取所有用户输入(恕我直言,如果弹出多个窗口一直要求我输入 1 个字段,我会很恼火),或者如果您仍然想惹恼您的最终用户,然后尝试使用Card Layout

标签: java swing joptionpane


【解决方案1】:

除了我上面的建议之外,还有一些其他内容需要了解以下代码(请在阅读代码部分之前全部阅读

  1. 阅读 layout manager 是什么以及它们是如何工作的,尤其是看看 ​​Grid LayoutBox Layout,如果您不理解本教程,请在 Google 上查看示例和解释。

  2. 了解methods 是什么以及它们是如何工作的。

  3. 了解Event Dispatch Thread (EDT) 及其function

  4. 注意不要混合控制台应用程序范式和 GUI 应用程序范式。使用其中一种。

  5. 学习How to use Dialogs

  6. 阅读 how to convert a String o a int 并了解如何转换为 double

  7. 对于您的boolean 字段,我将使用JRadioButton,包括ButtonGrouphow to get which radiobutton was selected in a buttongroup


这段代码应该为您自己完成它提供一个起点

  • annoyingGui 虽然较短,但不是我最喜欢的,因为每次您想从他们那里获得输入时,它都会为用户打开一个新对话框,这很烦人。

  • singleDialogInformation() 显示更复杂的 GUI,使用 JPanelGridLayout 请求用户信息和 BoxLayout 将其显示给用户,请注意我没有使用 2 个不同的变量,但将 pane 变量重新分配给具有不同布局的 JPanel 的新实例。


import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class UsingDialogsExample {

    private JFrame frame;
    private JPanel pane;
    private JTextField daysField;
    private JTextField assignmentField;
    private int days = 0;
    private int assignments = 0;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Comment / uncomment one of them to see the output related to each sample method.
//              new UsingDialogsExample().annoyingGui();
                new UsingDialogsExample().singleDialogInformation();
            }
        });
    }

    public void annoyingGui() {
        frame = new JFrame("My Frame's Title");

        String daysInput = JOptionPane.showInputDialog(frame, "How many days are left?"); //Get user input on the textfield as a String
        String assignmentsInput = JOptionPane.showInputDialog(frame, "How many assignments are due?");

        try {
            days = Integer.parseInt(daysInput); //Convert the string gotten above to an int
            assignments = Integer.parseInt(assignmentsInput);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }

        JOptionPane.showMessageDialog(frame, "The number of days left is: " + days);
        JOptionPane.showMessageDialog(frame, "The number of assignments due is: " + assignments);
    }

    public void singleDialogInformation() {
        pane = new JPanel();
        pane.setLayout(new GridLayout(0, 2, 2, 2));

        daysField = new JTextField(5);
        assignmentField = new JTextField(5);

        pane.add(new JLabel("How many days are left?"));
        pane.add(daysField);

        pane.add(new JLabel("How many assignments are due?"));
        pane.add(assignmentField);

        int option = JOptionPane.showConfirmDialog(frame, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.YES_OPTION) {

            String daysInput = daysField.getText();
            String assignmentsInput = assignmentField.getText();

            try {
                days = Integer.parseInt(daysInput);
                assignments = Integer.parseInt(assignmentsInput);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }

            pane = new JPanel();
            pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));

            pane.add(new JLabel("Days left: " + days));
            pane.add(new JLabel("Assignments due: " + assignments));

            JOptionPane.showMessageDialog(frame, pane);
        }
    }
}

annoyingGui的截图:

singleDialogInformation的截图:

【讨论】:

  • 如果我是为了快乐而这样做(特别是因为我比我必须使用的方法更了解这种方法),这种方法会非常有效,但遗憾的是我不是。不过感谢您的帮助,您提供的链接确实很有帮助。
  • 如果我这样做是为了快乐如果软件开发不能满足你,那么你不应该从事软件职业...... =/我希望我的回答给了你一个想法,如果这能解决你的问题,请务必accept
  • 你误会了,我确实喜欢编程。我的意思是,如果这是我的个人项目或休闲编程,那就完美了,只是对于这个任务,我们只能在程序中使用某些命令。
  • 好吧,我认为annoyingGui() 部分会满足您的要求(如果我没记错的话),您可以尝试一下,然后尝试自己改进它。我不是为你做你的全部作业,而是为你提供起点的想法,然后你需要实现它的逻辑,让它按照你的意愿工作,希望它至少有一点帮助。如果对每种方法有什么疑问,您可以阅读JOptionPane 文档here,掌握它们总是一个好主意
  • 那么要求是使用 JOptionPane。多个对话框窗口是我个人想要的,但我想我只需要把它吸起来,把所有的输入都放在一个窗口上。如果没有其他人有我正在寻找的东西(可能不存在),我一定会接受你的回答。
猜你喜欢
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
相关资源
最近更新 更多