简体   繁体   English

Java Swing Boxlayout:具有不同大小的面板(彼此之间的比率)

[英]Java Swing Boxlayout: having panels of different sizes (ratio to each other)

I'm trying to create a boxlayout panel with 2 other panels inside it, one of which is only half the size of the other (so a ratio of 1/3 to 2/3). 我正在尝试创建一个带有2个其他面板的boxlayout面板,其中一个面板的大小仅为另一个面板的一半(所以比例为1/3到2/3)。 Setting preferred size doesn't seem to work and I've been unable to figure out any other way (the following code was mainly generated with Windowbuilder, so apologies if it's a bit of a mess): 设置首选大小似乎不起作用,而且我也无法找出其他任何方法(以下代码主要是由Windowbuilder生成的,如果有点混乱,我们深表歉意):

public class GUIControls extends JFrame implements IGUIControls {
    private JTextField textField;

    private int posX=0,posY=0;

    public GUIControls() {

        getContentPane().setBackground(Color.WHITE);
        getContentPane().setLayout(new CardLayout(0, 0));

        JPanel rootPanel = new JPanel();
        rootPanel.setBorder(new LineBorder(new Color(0, 0, 255), 1, true));
        rootPanel.setBackground(Color.WHITE);
        getContentPane().add(rootPanel, "name_25253210045969");
        rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS));
        rootPanel.setPreferredSize(new Dimension(600, 400));

        JPanel menuLeft = new JPanel();
        rootPanel.add(menuLeft);
        menuLeft.setBackground(Color.WHITE);
        menuLeft.setLayout(null);
        menuLeft.setPreferredSize(new Dimension((1/3)*rootPanel.getWidth(), rootPanel.getHeight()));

        JPanel contentRight = new JPanel();
        rootPanel.add(contentRight);
        contentRight.setBackground(Color.WHITE);
        contentRight.setPreferredSize(new Dimension((2/3)*rootPanel.getWidth(), rootPanel.getHeight()));
        contentRight.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent arg0) {
                CardLayout cardLayout = (CardLayout) getContentPane().getLayout();
                cardLayout.show(getContentPane(), "name_36737116256884");
            }
        });
        contentRight.setLayout(null);

        setUndecorated(true);   

        addMouseListener(new MouseAdapter()
        {
           public void mousePressed(MouseEvent e)
           {
              posX=e.getX();
              posY=e.getY();
           }
        });

        addMouseMotionListener(new MouseAdapter()
        {
             public void mouseDragged(MouseEvent evt)
             {
                //sets frame position when mouse dragged            
                setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);

             }
        });

    }

    public void runGUI(){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUIControls guiControls = new GUIControls();
                guiControls.pack();
                guiControls.setLocationRelativeTo(null);
                guiControls.setSize(600, 400);
                guiControls.setVisible(true);
            }
        });
    }
}

Does anyone know how I'd go about doing this? 有人知道我会怎么做吗?

Thanks. 谢谢。

You could try using the GridBagLayout . 您可以尝试使用GridBagLayout Read the section from the Swing tutorial on How to Use GridBagLayout for more information. 阅读Swing教程中有关如何使用GridBagLayout的部分, 获取更多信息。 I would guess you would need to play with the weightx property. 我猜您将需要使用weightx属性。

Or you could use the Relative Layout which was specifically designed to do this. 或者,您可以使用专门用于此目的的相对布局

Edit: 编辑:

Did you do any debugging? 您是否进行了调试? What happens when you add: 添加时会发生什么:

System.out.println(menuLeft.getPreferredSize());
System.out.println(contentRight.getPreferredSize());

You have two problems: 您有两个问题:

  1. What is the value of 1/3? 1/3的值是多少? You need to fix your size calculation. 您需要修正尺寸计算。
  2. What is the size of your rootPanel? rootPanel的大小是多少? A component doesn't have a size until the component is displayed on the visible GUI. 在可见的GUI上显示组件之前,组件没有大小。 You set the preferred size of the component. 您设置组件的首选大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM