繁体   English   中英

JOptionPane每行有多个按钮?

[英]JOptionPane with multiple buttons on each line?

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

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

您可以将任何JComponents放置到JOptionPane ,在那里我看不到任何限制, JOptionPane是与JFrameJDialogJWindow相同的顶级容器 ,但与普通的Top-Level ContainersJOptionPane已经实现了来自内置的返回事件在Integer数值的功能中,意味着按钮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);

这是你想要的扫描仪,因为我可以得到你。

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获得它的方法。 在此示例中,单击的按钮文本用作窗格的输入值。 这是通过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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM