多个JOptionPane输入对话框? [英] Multiple JOptionPane input dialogs?

查看:129
本文介绍了多个JOptionPane输入对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找过去的一个小时,但是我一直无法找到我想要的解决方案.

I've been looking for the past hour, but I haven't been able to find the solution I am looking for.

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

I'm wanting to take multiple inputs from the user using JOptionPane, but I don't want them to all be in one dialog window. I'm wanting it to transition to the next or just make the next one pop up. Is there a way to do that using JOptionPane?

这是我到目前为止所拥有的:

Here's what I have so far:

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();

    }

}

推荐答案

除了我上面的建议之外,这里还有一些其他的东西需要理解下面的代码(在阅读代码部分之前,请先阅读所有内容)仅)

Apart from my above recommendations, here are some others that will be needed to understand the below code (PLEASE READ THEM ALL BEFORE GOING FOR THE CODE PART ONLY)

  1. 阅读布局管理器是什么以及它们的工作方式,尤其要看一下网格布局框布局,例如您不了解本教程.

  1. Read what a layout manager is and how they work, especially take a look at Grid Layout and Box Layout, Google for examples and explanations if you don't understand the tutorial.

阅读方法是什么以及如何他们工作.

Read what methods are and how they work.

了解有关事件调度线程(EDT)及其功能.

请注意不要混合使用控制台应用程序范例和GUI应用程序范例.使用另一个.

Be careful to not mix console application paradigm and GUI application paradigm. Use one or the other.

了解如何使用对话框

阅读如何转换String oa int 并查看如何转换为double.

Read how to convert a String o a int and look how to convert to double.

对于您的boolean字段,我将使用 ButtonGroup 如何以获得在按钮组中选择了哪个单选按钮:

For your boolean field I would use a JRadioButton including a ButtonGroup and how to get which radiobutton was selected in a buttongroup:


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


This code should give you a starting point on your way to finish it yourself

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

  • The annoyingGui while shorter, is not my favorite since it opens a new dialog for the user each time you want to get an imput from them, which is annoying.

singleDialogInformation()使用

The singleDialogInformation() displays a more complex GUI using a JPanel and GridLayout for requesting user information and a BoxLayout to show it back to the user, note that I'm not using 2 different variables, but reassigning the pane variable to a new instance of a JPanel with a different layout.

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的屏幕截图:

这篇关于多个JOptionPane输入对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆