问题
If the array you are using is, let's say a length of 15, and you want to display them all at once, it shows them side by side in a really long display box:
String[] options = {"Option 1","Option 2","Option 3","Option 4",
"Option 5","Option 6","Option 7","Option 8","Option 9",
"Option 10","Option 11","Option 12","Option 13","Option 14",
"Option 15"};
int displayoptions = JOptionPane.showOptionDialog (null, "select one", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options [0]);
Is there a way to display it so that it has a Flow Layout?
回答1:
For 15 options I'd do it more like this:
import java.awt.*;
import javax.swing.*;
public class ManyOptions {
ManyOptions() {
initUI();
}
public void initUI() {
String[] options = {"Option 1", "Option 2", "Option 3", "Option 4",
"Option 5", "Option 6", "Option 7", "Option 8", "Option 9",
"Option 10", "Option 11", "Option 12", "Option 13", "Option 14",
"Option 15"};
JComboBox combo = new JComboBox(options);
int result = JOptionPane.showConfirmDialog(
null,
combo,
"Select One",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("Selected Index: " + combo.getSelectedIndex());
} else {
System.out.println("choice cancelled..");
}
// old way, for comparison..
int displayoptions = JOptionPane.showOptionDialog(
null,
"select one",
"Title",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
options[0]);
System.out.println("displayoptions: " + displayoptions);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ManyOptions o = new ManyOptions();
}
};
SwingUtilities.invokeLater(r);
}
}
回答2:
It may be best to use a JPanel with a GridLayout, and display it in a modal JDialog (which is what a JOptionPane is in reality). For example,
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Foo1 {
private static final int BUTTON_COUNT = 15;
private static String selection = "";
public static void main(String[] args) {
JPanel panel = new JPanel(new GridLayout(3, 0, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for (int i = 0; i < BUTTON_COUNT; i++) {
panel.add(new JButton(new ButtonAction("Option " + (i + 1))));
}
JDialog dialog = new JDialog(null, "Select One", ModalityType.APPLICATION_MODAL);
dialog.add(panel);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
System.out.println("selection: " + selection);
}
private static class ButtonAction extends AbstractAction {
public ButtonAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
selection = e.getActionCommand();
Component c = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(c);
win.dispose();
}
}
}
来源:https://stackoverflow.com/questions/31307194/is-there-a-way-to-set-a-layout-for-joptionspane-showoptiondialog