我使用 JPanel 作为主面板来向用户显示信息。
我使用方法创建了另外 3 个 Jpanels。 titlePanel、verbiagePanel、closeButtonPanel。这些方法中的每一个都被指定为一个组件并添加到主面板中。我在主面板以及其他面板中使用 BoxLayout。
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
this.setTitle("EDI - Help Page");
//this.setResizable( false );
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(700, 300));
centerPanel.add(titlePanel, BorderLayout.NORTH);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel, BorderLayout.CENTER);
centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
centerPanel.add(closeButtonPanel, BorderLayout.SOUTH);
getContentPane().add(centerPanel);
this.pack();
其上方是主面板方法。当我编译并运行该对话框时,除了 verbiagePanel 之外,一切看起来都正确。它只有父面板和其他 2 个面板大小的一半。
这是我的 verbiagePanel 的代码
private JPanel verbiagePanel() {
String titleText = "<html><font size=4><b><center>How To Use This Application</b></center></font></html>";
String text = "<html>" +
" <P align=left><font size=3>" +
" 1. Add a data to the list of Selected Datasets.<br/>" +
" (see below for detailed instructions on adding datasets)<br/>" +
" 2. Select the project to send data to.<br/>" +
" 3. Adjust the list of selected 3D or 2D translators if desired.<br/>" +
" 4. Use the Send button to continue the EDI transaction." +
" </font></P>" +
" </html>";
JLabel titleLabel = new JLabel(titleText, JLabel.CENTER);
titleLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel bodyLabel = new JLabel(text, JLabel.LEFT);
bodyLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(titleLabel);
p.add(bodyLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
有趣的是,如果我从面板中删除 BoxLayout。该面板将扩展以匹配其他 2 个面板。但标签的间距太疯狂了。我最终会在面板中至少有 5 个标签。现在我只显示 2 个以使其更简单。
最佳答案
我最终使用了 GridBagLayout 模型。它按需要工作了。
关于java - 使用 Boxlayout 导致我的面板只有父面板大小的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830565/