我正在尝试像这样创建一个JOptionPane
。我尝试了很多布局管理器但是(GridBagLayout
,GridLayout
,AbsoluteLayout
)但我还没有成功。
例如,如果我使用GridBagLayout
,ipadx
确实有用,但JTextFields
宽度取决于文字长度。
如果我使用GridLayout
,则每个对象宽度都属于列宽。
如果我使用AbsoluteLayout
,则JOptionPane
不会将自身扩展到配置了携带对象的JPanel
边界。
我需要固定的对象宽度,高度和JOptionPane
在使用带有项目的JPanel
创建它时应该展开。
哪个布局管理器最适合此任务?如果你建议,我会专注于它。
- 编辑 -
GridLayout示例;
JPanel panel = new JPanel( new GridLayout(8, 2) );
JLabel inforForNameLabel = new JLabel();
inforForNameLabel.setText("Long Label Name....................................");
final JTextField userNameTextField = new JTextField();
userNameTextField.setText("aaaaaaaaaaaaa");
userNameTextField.setEditable(false);
JLabel inforForNameSurname = new JLabel();
inforForNameSurname.setText("Long Label Name..................................");
final JTextField NameSurnameTextField = new JTextField();
NameSurnameTextField.setText("aaaaaaaaaaaa aaaaaaaaaa");
JLabel inforForStatsCombobox = new JLabel();
inforForStatsCombobox.setText("Long Label Name.................................");
JComboBox statComboBox = new JComboBox();
DefaultComboBoxModel dt = new DefaultComboBoxModel();
statComboBox.setModel(dt);
dt.addElement("USER.....");
dt.addElement("ADMIN");
statComboBox.setSelectedItem("USER.....");
JLabel inforForAuthCombobox = new JLabel();
inforForAuthCombobox.setText("Long Label Name.................................");
String[] authComboBoxObjects = { "READ", "READ/WRITE"};
final JComboBox authComboBox = new JComboBox(authComboBoxObjects);
authComboBox.setSelectedItem("READ/WRITE");
panel.add(inforForNameLabel);
panel.add(userNameTextField);
panel.add(inforForNameSurname);
panel.add(NameSurnameTextField);
panel.add(inforForStatsCombobox);
panel.add(statComboBox);
panel.add(inforForAuthCombobox);
panel.add(authComboBox);
String[] buttons = { "EXIT", "OK" };
int rc = JOptionPane.showOptionDialog(null, panel,"User Config", JOptionPane.INFORMATION_MESSAGE,JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[1]);
if(rc == 0 || rc == -1){
System.out.println(userNameTextField.getText());
}
else{
System.out.println("Cancel...");
}
答案 0 :(得分:0)
同意JDialog为您提供全面控制。
我需要知道如何通过JOptionPane
制作相同的东西
UIManager似乎控制了最小尺寸。在显示选项窗格之前,您可以使用以下方式覆盖:
Dimension size = UIManager.getDimension("OptionPane.minimumSize");
size.width = 100;
UIManager.put("OptionPane.minimumSize", size);
您可以查看UIManager Defaults以获取由UIManager控制的其他属性的列表。请注意,并不能保证所有LAF都能管理相同的属性。
答案 1 :(得分:0)
感谢@ControlAltDel和@Gorbles
的路由评论下面的代码在问题中构建了所需格式的OptionPane;
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel inforForNameLabel = new JLabel();
inforForNameLabel.setText("Long Label Name.............................");
inforForNameLabel.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForNameLabel2 = new JLabel();
inforForNameLabel2.setText("Long Label Name............................");
inforForNameLabel2.setAlignmentX(LEFT_ALIGNMENT);
final JTextField userNameTextField = new JTextField();
userNameTextField.setText("aaaaaaaaaaaaa");
userNameTextField.setEditable(false);
userNameTextField.setPreferredSize(new Dimension(345, 30));
userNameTextField.setMaximumSize(new Dimension(345, 30));
userNameTextField.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForNameSurname = new JLabel();
inforForNameSurname.setText("Long Label Nameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
inforForNameSurname.setAlignmentX(LEFT_ALIGNMENT);
final JTextField NameSurnameTextField = new JTextField();
NameSurnameTextField.setText("aaaaaaaaaaaa aaaaaaaaaa");
NameSurnameTextField.setPreferredSize(new Dimension(345, 30));
NameSurnameTextField.setMaximumSize(new Dimension(345, 30));
NameSurnameTextField.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForStatsCombobox = new JLabel();
inforForStatsCombobox.setText("Long Label Name....................");
inforForStatsCombobox.setAlignmentX(LEFT_ALIGNMENT);
JComboBox statComboBox = new JComboBox();
DefaultComboBoxModel dt = new DefaultComboBoxModel();
statComboBox.setModel(dt);
dt.addElement("USER.....");
dt.addElement("ADMIN");
statComboBox.setSelectedItem("USER.....");
statComboBox.setPreferredSize(new Dimension(200, 30));
statComboBox.setMaximumSize(new Dimension(200, 30));
statComboBox.setAlignmentX(LEFT_ALIGNMENT);
JLabel inforForAuthCombobox = new JLabel();
inforForAuthCombobox.setText("Long Label Name................................");
inforForAuthCombobox.setAlignmentX(LEFT_ALIGNMENT);
String[] authComboBoxObjects = { "READ", "READ/WRITE"};
final JComboBox authComboBox = new JComboBox(authComboBoxObjects);
authComboBox.setSelectedItem("READ/WRITE");
authComboBox.setPreferredSize(new Dimension(200, 30));
authComboBox.setMaximumSize(new Dimension(200, 30));
authComboBox.setAlignmentX(LEFT_ALIGNMENT);
panel.add(inforForNameLabel);
panel.add(inforForNameLabel2);
panel.add(userNameTextField);
panel.add(inforForNameSurname);
panel.add(NameSurnameTextField);
panel.add(inforForStatsCombobox);
panel.add(statComboBox);
panel.add(inforForAuthCombobox);
panel.add(authComboBox);
String[] buttons = { "EXIT", "OK" };
int rc = JOptionPane.showOptionDialog(null, panel,"User Config", JOptionPane.INFORMATION_MESSAGE,JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[1]);
if(rc == 0 || rc == -1){
System.out.println("Cancel...");
}
else{
System.out.println(userNameTextField.getText());
}