【问题标题】:JOptionPane with multiple buttons on each line?JOptionPane 每行有多个按钮?
【发布时间】:2012-01-29 07:45:45
【问题描述】:

如何在每行显示带有多个 JButtonsJOptionPane.showinputDialog()?我不是在说YesNoCancel 按钮,而是在JOptionPane.showinputDialog 的内容区域中显示的多个自定义标记JButtons

所以我还需要从JOptionPane 获取按下按钮的值。

【问题讨论】:

标签: java swing jbutton joptionpane


【解决方案1】:

您可以将任何JComponents 放在JOptionPane 上,我看不到任何限制,JOptionPaneJFrameJDialogJWindow 相同,但与普通的Top-Level ContainersJOptionPaneInteger 值中实现了来自内置函数的返回事件,也就是按钮YESNOOKCANCELCLOSE

将所有 JButton 放入数组中

String[] buttons = { "Yes", "Yes to all", "No", "Cancel".... };    
int returnValue = JOptionPane.showOptionDialog(null, "Narrative", "Narrative",
        JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[i]);
System.out.println(returnValue);

【讨论】:

  • 这个问题是当 joption 显示时我的应用程序似乎挂起......就像它是透明的并且应用程序冻结在这条线上。
  • 存在三个问题 1) 用于关闭 JOptionPane 的代码超出 EDT(您只能看到带有关闭按钮的工具栏),2) 用于关闭 JOptionPane 的代码已链接/miss_linked 到空实例,3) JOptionPane 被处理,然后 int 返回 NPE,现在您有两个选择 a) 使用演示冻结的代码编辑此主题,b) 创建一个新帖子 c) 也许有/是另一个关于的主题
  • 我使用invokeLater() 修复了它。好的,我现在遇到的问题是按钮并排出现,而不是每个新行一个。
  • @Kim Jong Woo maaaan 你做了什么:-)
  • 我认为按钮会出现在每个新行上。事实并非如此。
【解决方案2】:

这是我能得到的最接近你想要的。

Object[] colours = {"Blue", "Red", "Black", "White"};

int n = JOptionPane.showOptionDialog(null,
    "Which colour do you like?",
    "Choose a colour",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    colours,
    colours[0]);

System.out.println("The users likes " + colours[n]);

【讨论】:

  • 同样的问题,应用程序在此行崩溃。 joptionpane 窗口显示,但没有显示对话框的内容。
【解决方案3】:

您希望按钮位于不同的行上?这是一种使用JOptionPane 的方法。在此示例中,单击按钮的文本用作窗格的输入值。这是通过在createChoiceAction() 中创建的操作完成的。多行按钮面板成为JOptionPane构造函数中提供的“消息”。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;


public class JOptionPaneExample {
    public static final String DIALOG_NAME = "what-dialog";
    public static final String PANE_NAME = "what-pane";

    private void showDialog() {
        JOptionPane pane = new JOptionPane(createInputComponent());
        pane.setName(PANE_NAME);
        JDialog dialog = pane.createDialog("What?");
        dialog.setName(DIALOG_NAME);
        dialog.setSize(400, 400);
        dialog.setVisible(true);
        System.out.println(pane.getInputValue());
        System.exit(0);
    }

    private JComponent createInputComponent() {
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JLabel("Pick a Category:"), BorderLayout.NORTH);

        Box rows = Box.createVerticalBox();
        ActionListener chooseMe = createChoiceAction();

        Map<String, List<String>> inputs =
                new LinkedHashMap<String, List<String>>();
        inputs.put("Cars:", Arrays.asList("Toyota", "Honda", "Yugo"));
        inputs.put("Phones:", Arrays.asList("iPhone", "Android", "Rotary"));
        inputs.put("Pets:", Arrays.asList("Dog", "Cat", "Sock"));

        for (String category : inputs.keySet()) {
            JPanel b = new JPanel(new FlowLayout(FlowLayout.LEADING));
            b.add(new JLabel(category));

            for (String choice : inputs.get(category)) {
                JButton button = new JButton(choice);
                button.addActionListener(chooseMe);
                b.add(button);
            }
            rows.add(Box.createVerticalStrut(10));
            rows.add(b);
        }

        rows.add(Box.createVerticalStrut(600));

        p.add(rows, BorderLayout.CENTER);
        return p;
    }

    private ActionListener createChoiceAction() {
        ActionListener chooseMe = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton choice = (JButton) e.getSource();

                // find the pane so we can set the choice.
                Container parent = choice.getParent();
                while (!PANE_NAME.equals(parent.getName())) {
                    parent = parent.getParent();
                }

                JOptionPane pane = (JOptionPane) parent;
                pane.setInputValue(choice.getText());

                // find the dialog so we can close it.
                while ((parent != null) &&
                        !DIALOG_NAME.equals(parent.getName()))
                { parent = parent.getParent(); }

                if (parent != null) {
                    parent.setVisible(false);
                }
            }
        };
        return chooseMe;
    }

    public static void main(String[] args) {
        JOptionPaneExample main = new JOptionPaneExample();
        main.showDialog();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    相关资源
    最近更新 更多