[英]JOptionPane with multiple buttons on each line?
我如何在每一行上显示一个带有多个JButtons
的JOptionPane.showinputDialog()
? 我不是在谈论Yes
, No
, Cancel
按钮,而是在JOptionPane.showinputDialog
的内容区域中显示多个自定义标记的JButtons
?
所以我需要从JOptionPane
获取按下按钮的值。
您可以将任何JComponents
放置到JOptionPane ,在那里我看不到任何限制, JOptionPane
是与JFrame
, JDialog
或JWindow
相同的顶级容器 ,但与普通的Top-Level Containers
, JOptionPane
已经实现了来自内置的返回事件在Integer
数值的功能中,意味着按钮YES
, NO
, OK
, CANCEL
和CLOSE
也是,
将所有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.