多个JOptionPane输入对话框?

时间:2017-01-27 22:55:58

标签: java swing joptionpane

我一直在寻找过去的一小时,但我还没能找到我想要的解决方案。

我想使用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();

    }

}

1 个答案:

答案 0 :(得分:5)

除了我的上述建议之外,还有一些其他需要了解下面的代码(请在执行代码之前阅读所有内容

  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

  8. 此代码应该为您提供自己完成的起点

    • annoyingGui虽然较短,但不是我的最爱,因为每当你想要从他们那里得到一个输入时它会为用户打开一个新的对话框,这很烦人。

    • singleDialogInformation()使用JPanelGridLayout显示更复杂的GUI,用于请求用户信息,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的屏幕截图:

    enter image description here enter image description here

    singleDialogInformation的屏幕截图:

    enter image description here enter image description here