如何在每行显示多个JOptionPane.showinputDialog()
的{{1}}?我不是在谈论JButtons
,Yes
,No
按钮,而是在Cancel
的内容区域中显示多个自定义标记JButtons
?
所以我需要从JOptionPane.showinputDialog
按下按钮的值。
答案 0 :(得分:6)
您可以将JComponents
放置到JOptionPane,我看不到任何限制,JOptionPane
与JFrame
相同Top-Level Container,{{ 1}}或JDialog
,但与普通JWindow
相比,Top-Level Containers
已在JOptionPane
值的内置函数中实现了返回事件,这意味着按钮Integer
,YES
,NO
,OK
和CANCEL
,
将所有JButton放入数组
CLOSE
答案 1 :(得分:5)
这和我想要的一样接近你的意思。
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]);
答案 2 :(得分:1)
你想要分开的按钮吗?以下是使用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();
}
}