Java 如何在GridBagLayout的左上角单元格中放置包含三个按钮的面板?

Java 如何在GridBagLayout的左上角单元格中放置包含三个按钮的面板?,java,swing,Java,Swing,我有一个包含三个按钮的面板。我希望将此面板放置在GridBagLayout的左上角单元格中 我的问题是,当我运行程序时,面板不是位于左上角的单元格中,而是位于布局的中间。我已将gridx和gridy设置为零 private JFrame frame; private JMenuBar menuBar; private JMenu menuFile; private JMenuItem fileItem1; private JMenuItem fileItem2; private JPanel

我有一个包含三个按钮的面板。我希望将此面板放置在
GridBagLayout
的左上角单元格中

我的问题是,当我运行程序时,面板不是位于左上角的单元格中,而是位于布局的中间。我已将
gridx
gridy
设置为零

private JFrame frame;

private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem fileItem1;
private JMenuItem fileItem2;

private JPanel btnPanel;
private JButton btnRewind;
private JButton btnPlayPause;
private JButton btnFastForward;

private static boolean shouldFill = true;
private static boolean shouldWeightX = true;
private static boolean RIGHT_TO_LEFT = false;

public JPlayer() {

}

public void createAndShowGUI() {
    frame = new JFrame();
    frame.setTitle("JPlayer");
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

    frame.setVisible(true);
}

private void addComponentsToPane(Container pane) {
    if(RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    if(shouldFill) {
        gbc.fill = GridBagConstraints.HORIZONTAL;
    }

    btnRewind = new JButton();
    try {
        Image imgRewind = ImageIO.read(getClass().getResource("../utils/images/rewind.png"));

        btnRewind.setIcon(new ImageIcon(imgRewind));
        btnRewind.setOpaque(true);
        btnRewind.setContentAreaFilled(false);
        btnRewind.setBorderPainted(false);
    } catch(Exception e) {
        e.printStackTrace();
    }

    btnPlayPause = new JButton();
    try {
        Image imgPlay = ImageIO.read(getClass().getResource("../utils/images/play.png"));
        Image imgPause = ImageIO.read(getClass().getResource("../utils/images/pause.png"));

        btnPlayPause.setIcon(new ImageIcon(imgPlay));
        btnPlayPause.setOpaque(true);
        btnPlayPause.setContentAreaFilled(false);
        btnPlayPause.setBorderPainted(false);
    } catch(Exception e) {
        e.printStackTrace();
    }

    btnFastForward = new JButton();
    try {
        Image imgFastForward = ImageIO.read(getClass().getResource("../utils/images/fast_forward.png"));

        btnFastForward.setIcon(new ImageIcon(imgFastForward));
        btnFastForward.setOpaque(true);
        btnFastForward.setContentAreaFilled(false);
        btnFastForward.setBorderPainted(false);
    } catch(Exception e) {
        e.printStackTrace();
    }

    btnPanel = new JPanel();
    btnPanel.add(btnRewind);
    btnPanel.add(btnPlayPause);
    btnPanel.add(btnFastForward);
    btnPanel.setSize(new Dimension(40, 40));
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;

    JButton btn = new JButton("Some Button");
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 0;

    pane.add(btnPanel, gbc);
    pane.add(btn, c);
}
尝试将默认值作为“中心定位”的约束

直接从

如果组件的显示区域大于组件本身,则可以使用
GridBagConstraints.anchor
约束指定组件在显示区域中的位置

锚定约束的值可以是绝对的(北、南、东、西等),也可以是相对的方向(在页面开始、行结束、第一行开始等),或者相对于组件的基线

注意

  • 您不需要使用GridBagConstraints的多个实例。在面板中添加构件之前,只需创建一个并更新约束即可。通过这种方式,您可以重用对所有人都通用的现有约束

  • 不要忘记设置
    weightx
    weighty
    以正确使用
    anchor
    约束

  • 直接来自同一文档

    weightx,weighty

    指定权重是一门艺术,它会对GridBagLayout控件中组件的外观产生重大影响。权重用于确定如何在列(权重x)和行(权重y)之间分配空间;这对于指定大小调整行为很重要

    除非为weightx或weighty指定至少一个非零值,所有组件都聚集在其容器的中心。这是因为当权重为0.0(默认值)时,GridBagLayout会在其单元格网格和容器边缘之间放置任何额外的空间

    尝试将默认值作为“中心定位”的约束

    直接从

    如果组件的显示区域大于组件本身,则可以使用
    GridBagConstraints.anchor
    约束指定组件在显示区域中的位置

    锚定约束的值可以是绝对的(北、南、东、西等),也可以是相对的方向(在页面开始、行结束、第一行开始等),或者相对于组件的基线

    注意

  • 您不需要使用GridBagConstraints的多个实例。在面板中添加构件之前,只需创建一个并更新约束即可。通过这种方式,您可以重用对所有人都通用的现有约束

  • 不要忘记设置
    weightx
    weighty
    以正确使用
    anchor
    约束

  • 直接来自同一文档

    weightx,weighty

    指定权重是一门艺术,它会对GridBagLayout控件中组件的外观产生重大影响。权重用于确定如何在列(权重x)和行(权重y)之间分配空间;这对于指定大小调整行为很重要

    除非为weightx或weighty指定至少一个非零值,所有组件都聚集在其容器的中心。这是因为当权重为0.0(默认值)时,GridBagLayout会在其单元格网格和容器边缘之间放置任何额外的空间