使用Boxlayout导致我的面板只有父面板的一半大小

时间:2012-08-06 14:44:27

标签: java html swing jlabel boxlayout

我使用JPanel作为主面板向我的用户显示信息。

我已经使用方法创建了3个其他Jpanel。 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之外,一切看起来都正确。它只是父面板和其他两个面板的一半大小。

enter image description here

以下是我的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>" +
        "   &nbsp;&nbsp;&nbsp;&nbsp;1. Add a data to the list of Selected Datasets.<br/>" +
        "   &nbsp;&nbsp;&nbsp;&nbsp;(see below for detailed instructions on adding datasets)<br/>" +
        "   &nbsp;&nbsp;&nbsp;&nbsp;2. Select the project to send data to.<br/>" +
        "   &nbsp;&nbsp;&nbsp;&nbsp;3. Adjust the list of selected 3D or 2D translators if desired.<br/>" +
        "   &nbsp;&nbsp;&nbsp;&nbsp;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;   
}    

有趣的是,如果我从Panel中删除BoxLayout。小组将扩大以匹配其他两个小组。但标签的间距是疯狂的。我最终会在面板中至少有5个标签。现在我只显示2,使其更简单。

1 个答案:

答案 0 :(得分:0)

我最终使用了GrigBagLayout模型。它根据需要起作用。