问题
can't get the buttons in the OptionDialog to appear on a new line. They all appear in one row, but I'd like to have them on separate lines.
I also tried setting up a frame to add to the OptionDialog (to set the max width), but it didn't work out for me either.
Any ideas/help/suggestions appreciated.
Object[] options = { "Button1", "Button2", "Button3", "Button4",
"Button5 On a newLine\n\n", "Button 6", "Button 7" };
int x = JOptionPane.showOptionDialog(null, "Choose a button..", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
回答1:
Consider this alternative.
import javax.swing.*;
class Options {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
Object[] options = {
"Option 1",
"Option 2",
"Option 3",
"Option 4",
"Option 5",
"Option 6",
"Option 7",
"None of the above"
};
JComboBox optionList = new JComboBox(options);
optionList.setSelectedIndex(7);
JOptionPane.showMessageDialog(null, optionList, "Title",
JOptionPane.QUESTION_MESSAGE);
}
});
}
}
回答2:
You can't do that using the Option Dialog from JOptionPane, but you still can create your own dialog window by extending JDialog, and this way you will be able to use the layout you want for your components.
回答3:
Create your own OptionPane class if you want to break buttons in multiple lines.
You'll be breaking a bunch of UI standards in doing so, though.
回答4:
The same kind of answer as above, but more concrete:
Object[] options = outputcdirs;
JComboBox optionList = new JComboBox(outputcdirs);
optionList.setSelectedIndex(0);
JPanel jpan = new JPanel ();
jpan.add(new JLabel("Select dirs:"));
jpan.add(optionList);
int n = JOptionPane.showOptionDialog(this, jpan, "text...",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
null,
null);
if (n != -1)
n = optionList.getSelectedIndex();
if (n == -1)
throw new Exception("No selection: ...");
String value = outputcdirs[n];
来源:https://stackoverflow.com/questions/9535748/showoptiondialog-buttons-on-separate-lines