【问题标题】:BoxLayout refuses to honor preferred size of JButtonBoxLayout 拒绝尊重 JButton 的首选大小
【发布时间】:2012-12-22 23:49:40
【问题描述】:

我一直在做一个模拟赌博游戏的小项目。不幸的是,我在使用BoxLayout 时遇到了一些奇怪的问题。据我所知,LayoutManagers 通常会尊重任何组件的首选大小。但是,在下面的代码中,BoxLayout 没有。

到目前为止,这是我的代码:

import java.awt.*;
import javax.swing.*;



public class Main 
{
    public static void main(String[] args)
    {
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Suit-Up");
      frame.setContentPane(makeGUI());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(900,450);
      frame.setLocationRelativeTo(null);
      frame.setResizable(false);
      frame.setVisible(true);
    }

    public static JPanel makeGUI()
    {
      JPanel main = new JPanel();
      main.setMinimumSize(new Dimension(900,450));
      main.setBackground(Color.red);

      JPanel infoPanel = new JPanel();
      infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
      infoPanel.setPreferredSize(new Dimension(900,60));
      infoPanel.setBackground(Color.green);
      main.add(infoPanel);

      JPanel infoText = new JPanel();
      infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS));
      infoPanel.add(infoText);

      JPanel moneyText = new JPanel();
      moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS));
      infoText.add(moneyText);

      JPanel lastGameText = new JPanel();
      lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS));
      infoText.add(lastGameText);

      JButton playAgain = new JButton("Play Again ($20)");
      playAgain.setPreferredSize(new Dimension(200,60));
      infoPanel.add(playAgain);

      JButton finish = new JButton("End Session");
      finish.setPreferredSize(new Dimension(200,60));
      infoPanel.add(finish);

      JPanel cardPanel = new JPanel();
      cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS));
      main.add(cardPanel);

      return main;
    }
}

尽管为JButtons 指定了首选大小,但它们不会更改其大小。我也试过setMaximumSize()setMinimumSize(),但都没有任何效果。

我是否忽略了一些明显的东西,或者这是 BoxLayout 的限制?

【问题讨论】:

  • 你可以一直使用BoxLayout,但是嵌套组件...
  • 抱歉,我不确定您所说的嵌套是什么意思。我对布局管理器比较陌生。
  • 如果您将按钮放在面板上,并将面板而不是按钮直接添加到容器中,则面板将被调整大小而不是按钮。

标签: java swing jpanel jbutton layout-manager


【解决方案1】:

“据我所知,LayoutManagers 通常会尊重任何组件的首选大小” - 这实际上是不正确的。首选/最小/最大大小只是布局管理器可以用来确定如何最好地布局内容的“提示”。如果他们愿意,布局管理器可以简单地忽略它们。

来自 JavaDocs

BoxLayout 尝试以组件的首选宽度排列组件 (用于水平布局)或高度(用于垂直布局)。为一个 水平布局,如果不是所有组件的高度相同, BoxLayout 试图让所有组件都达到最高 零件。如果这对于特定组件是不可能的,那么 BoxLayout 垂直对齐该组件,根据 组件的 Y 对齐。默认情况下,组件的 Y 对齐方式为 0.5,这意味着组件的垂直中心应该与其他组件的垂直中心具有相同的Y坐标 0.5 Y 对齐。

同样,对于垂直布局,BoxLayout 尝试使所有 列中的组件与最宽的组件一样宽。如果说 失败,它会根据它们的 X 对齐方式水平对齐它们。 对于 PAGE_AXIS 布局,水平对齐是基于 组件的前沿。换句话说,X对齐值 如果容器的 ComponentOrientation 是从左到右,则 0.0 表示组件的左边缘,它表示组件的右边缘 否则组件。

【讨论】:

  • 不过,我使用的首选尺寸应该没有任何问题。在JPanel 中有足够的空间可以容纳JButtons,他们只是拒绝对其大小进行任何修改,而是在其标签中添加更多文本。我已阅读文档,但没有看到任何解释我现在遇到的问题。
  • @Thrfoot 我运行了你的测试代码,BoxLayout 只是拒绝使用JButtonpreferredSize,不管我尝试了什么。通常不建议以这种方式操作组件的首选大小,通常鼓励我们使用布局管理器或直接覆盖 getPreferredSize 方法。奇怪的是,FlowLayout 工作得很好......
  • 那很奇怪......好吧,我想我将无法让它与BoxLayout一起工作。你会推荐任何其他我可以得到或多或少类似效果的布局管理器吗?
  • @Thrfoot 看到您将宽度和高度设置为相等,请尝试使用向左对齐的FlowLayout...
  • 做到了!谢谢!我想有时候事情是行不通的。
猜你喜欢
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 2014-11-17
  • 2014-04-30
  • 1970-01-01
  • 2020-03-14
  • 2016-07-02
  • 2017-10-14
相关资源
最近更新 更多