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

标签 java swing

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

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

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其默认值为 CENTER 定位的约束。

直接来自 How to Use GridBagLayout 上的文档

If a component's display area is larger than the component itself, then you can specify whereabouts in the display area the component will be displayed by using the GridBagConstraints.anchor constraint.

The anchor constraint's values can be absolute (north, south, east, west, and so on), or orientation-relative (at start of page, at end of line, at the start of the first line, and so on), or relative to the component's baseline.

enter image description here

注意:

  1. 您不需要使用 GridBagConstraints 的多个实例。只需创建一个并更新约束,然后再将组件添加到面板中即可。通过这种方式,您可以重用所有人都通用的现有约束。

  2. 不要忘记设置 weightxweighty 以正确使用anchor 约束。

直接来自同一文档

权重x,权重

Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

关于java - 如何在 GridBagLayout 的左上角单元格中放置包含三个按钮的面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588066/

相关文章:

java - 从二进制数据中提取某些位

java - java jframe中的按钮不会调整大小?

java - 如何使用 TextWatcher 更新相同的 EditText?

未找到 Java 配置/j_spring_security_check

java - 如何将任务分配给Java线程?

java - Java实现定时刷新Cache

java - 有人可以告诉我 java swing 的错误吗?

Java、Swing、获取和更改所有输入字段

java - 将 JButton 添加到 JTable

java - 如何通过 JOptionPane 捕获的用户输入将数据添加到 JList?