簡體   English   中英

多個JOptionPane輸入對話框?

[英]Multiple JOptionPane input dialogs?

我一直在尋找過去的一個小時,但是我一直找不到想要的解決方案。

我想使用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. 閱讀布局管理器及其工作原理,尤其是在不了解本教程的情況下,請閱讀Google的Grid LayoutBox Layout以獲得示例和說明。

  2. 閱讀什么是方法以及它們如何工作。

  3. 閱讀有關事件調度線程(EDT)及其功能的信息

  4. 注意不要混合使用控制台應用程序范例和GUI應用程序范例。 使用一個或另一個。

  5. 了解如何使用對話框

  6. 閱讀如何將String轉換為int並查看如何轉換為double

  7. 對於您的boolean字段,我將使用一個包含ButtonGroupJRadioButton以及如何獲取在buttongroup中選擇哪個單選按鈕的方法


此代碼應為您自己完成此方法提供一個起點

  • 較短的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屏幕截圖:

在此處輸入圖片說明 在此處輸入圖片說明

singleDialogInformation屏幕截圖:

在此處輸入圖片說明 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM