BoxLayout refuses to honor preferred size of JButton

混江龙づ霸主 提交于 2020-01-21 15:26:45

问题


I have been working on a small project that is supposed to simulate a gambling game. Unfortunately, I ran into some odd issues while working with BoxLayout. To the best of my knowledge, LayoutManagers usually honor any component's preferred size. However, in the below code, BoxLayout does not.

Here is my code thus far:

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;
    }
}

Despite specifying preferred sizes for both JButtons, they do not change their sizes. I have tried setMaximumSize() and setMinimumSize() as well, but neither had any effect.

Am I overlooking something obvious, or is this a limitation of BoxLayout?


回答1:


"To the best of my knowledge, LayoutManagers usually honor any component's preferred size" - That's actually not true. preferred/min/max size are just "hints" that layout managers MAY use to determine how best to layout there contents. Layout managers are allowed to simply ignore them if they want to.

From the JavaDocs

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise.



来源:https://stackoverflow.com/questions/14226622/boxlayout-refuses-to-honor-preferred-size-of-jbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!