BorderLayout center command won't center

2020-04-16 05:16发布

I am trying to place two JButtons next to each other in the center of a JFrame, that won't re-size the buttons when the JFrame is re-sized.

To do this I placed the two buttons in a panel with a FlowLayout, that is then placed in a panel with a center BorderLayout.

However the following code won't display the chosen panel in the center of the BorderLayout.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class test extends JFrame {

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JButton button1 = new JButton("one");
    private JButton button2 = new JButton("two");

    public test() {
        panel1.setLayout(new FlowLayout());
        panel1.add(button1);
        panel1.add(button2);

        panel2.setLayout(new BorderLayout());
        panel2.add(panel1, BorderLayout.CENTER);

        this.add(panel2);
    }

    public static void main(String[] args) {
        test frame = new test();
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

3条回答
劫难
2楼-- · 2020-04-16 05:16

Set GridBagLayout to panel1.

 panel1.setLayout(new GridBagLayout());

EDIT:

@trashgod : I have to figure out how the default constraints do it.

Because of the field GridBagLayout.defaultConstraints :

This field holds a gridbag constraints instance containing the default values, so if a component does not have gridbag constraints associated with it, then the component will be assigned a copy of the defaultConstraints.

In normal practice, one must have to create GridBagConstraints object and set fields to specify the constraints on each object.

To quote the tutorial:

The preferred approach to set constraints on a component is to use the Container.add variant, passing it a GridBagConstraints object

查看更多
相关推荐>>
3楼-- · 2020-04-16 05:23

JPanel uses FlowLayout by default, and FlowLayout uses centered alignment by default... This seems easier to me than going to GridBagLayout or even BoxLayout. If you don't want the buttons to 'wrap' when the panel gets too small for them, you could set a minimum size.

package example;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FlowLO extends JFrame
{
    public static void main(String[] args)
    {
        FlowLO flowLO = new FlowLO();
        flowLO.go();
    }
    public void go()
    {
        JPanel centeredPanel = new JPanel();
        centeredPanel.add(new JButton("one"));
        centeredPanel.add(new JButton("two"));
        getContentPane().add(centeredPanel);
        pack();
        setVisible(true);
    }
}
查看更多
够拽才男人
4楼-- · 2020-04-16 05:24

Unintuitive as it seems, it is behaving correctly.

panel1 is assigned as much screen space as is available, because it is the only component inside panel2. FlowLayout then lays out components starting from the top of the available space, and only puts components further down once it has filled all the horizontal space available. Thus, you get your two buttons at the top of the frame.

You could try using a Box instead:

public class test extends JFrame {

    private JComponent box = Box.createHorizontalBox();
    private JButton button1 = new JButton("one"); 
    private JButton button2 = new JButton("two"); 

    public test() {
        box.add(Box.createHorizontalGlue());
        box.add(button1);
        box.add(button2);
        box.add(Box.createHorizontalGlue());
        this.add(box);
    }

    ...
}

A horizontal box automatically centers components vertically, and the two glue components take up any extra horizontal space available so that the buttons sit in the center of the box.

查看更多
登录 后发表回答