Frage

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). 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):

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.

War es hilfreich?

Lösung

You could try using the GridBagLayout. Read the section from the Swing tutorial on How to Use GridBagLayout for more information. I would guess you would need to play with the weightx property.

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? You need to fix your size calculation.
  2. What is the size of your rootPanel? A component doesn't have a size until the component is displayed on the visible GUI. You set the preferred size of the component.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top