简单的Java GUI作为弹出窗口和下拉菜单

Ser*_*erg 5 java swing popup joptionpane jcombobox

我从来没有在java中编写GUI.这次我也可以跳过它并args用作UI(用户界面).但我想知道是否有一种简单的方法可以创建一个小的GUI来让用户选择其中一个选项.换句话说,要实现askUser()用户可以从下拉菜单中选择并按"确定"的功能.我花了一些时间学习这个主题,但甚至不确定我知道这个任务需要哪种类型的GUI.JFrame的?JPanel的?JMenu的?谢谢.

这是所需功能的一个例子.

package trygui;

public class Main {

    public static void main(String[] args) {
        String[] choices = new String[]{"cats", "dogs"};
        int choice = askUser(choices);
        System.out.println("selected: " + choices[choice]);
    }

    static int askUser(String[] choices) {
        // create pop-up dialog
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:我使用Netbeans,如果这可以有所作为.

Mad*_*mer 14

最简单的选择是使用JOptionPaneAPI

在此输入图像描述

public class TestOptionPane03 {

    public static void main(String[] args) {
        new TestOptionPane03();
    }

    public TestOptionPane03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel panel = new JPanel();
                panel.add(new JLabel("Please make a selection:"));
                DefaultComboBoxModel model = new DefaultComboBoxModel();
                model.addElement("Chocolate");
                model.addElement("Strewberry");
                model.addElement("Vanilla");
                JComboBox comboBox = new JComboBox(model);
                panel.add(comboBox);

                int result = JOptionPane.showConfirmDialog(null, panel, "Flavor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                switch (result) {
                    case JOptionPane.OK_OPTION:
                        System.out.println("You selected " + comboBox.getSelectedItem());
                        break;
                }

            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过阅读如何制作对话来了解更多信息

更新了反馈

public class TestOptionPane03 {

    public static void main(String[] args) {
        String choice = ask("Chocolate", "Strewberry", "Vanilla");
        System.out.println("You choose " + choice);
    }

    public static String ask(final String... values) {

        String result = null;

        if (EventQueue.isDispatchThread()) {

            JPanel panel = new JPanel();
            panel.add(new JLabel("Please make a selection:"));
            DefaultComboBoxModel model = new DefaultComboBoxModel();
            for (String value : values) {
                model.addElement(value);
            }
            JComboBox comboBox = new JComboBox(model);
            panel.add(comboBox);

            int iResult = JOptionPane.showConfirmDialog(null, panel, "Flavor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            switch (iResult) {
                case JOptionPane.OK_OPTION:
                    result = (String) comboBox.getSelectedItem();
                    break;
            }

        } else {

            Response response = new Response(values);
            try {
                SwingUtilities.invokeAndWait(response);
                result = response.getResponse();
            } catch (InterruptedException | InvocationTargetException ex) {
                ex.printStackTrace();
            }

        }

        return result;

    }

    public static class Response implements Runnable {

        private String[] values;
        private String response;

        public Response(String... values) {
            this.values = values;
        }

        @Override
        public void run() {
            response = ask(values);
        }

        public String getResponse() {
            return response;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)