有没有办法为 JOptionsPane.showOptionDialog 设置布局?
Is there a way to set a layout for JOptionsPane.showOptionDialog?
如果您使用的数组,比方说长度为 15,并且您想一次显示它们,它会在一个很长的显示框中并排显示它们:
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]);
有没有办法显示它以使其具有流式布局?
最好将 JPanel 与 GridLayout 一起使用,并将其显示在模式 JDialog 中(实际上是 JOptionPane)。例如,
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();
}
}
}
对于 15 个选项,我更愿意这样做:
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);
}
}
如果您使用的数组,比方说长度为 15,并且您想一次显示它们,它会在一个很长的显示框中并排显示它们:
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]);
有没有办法显示它以使其具有流式布局?
最好将 JPanel 与 GridLayout 一起使用,并将其显示在模式 JDialog 中(实际上是 JOptionPane)。例如,
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();
}
}
}
对于 15 个选项,我更愿意这样做:
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);
}
}