首页 > 解决方案 > GridBagLayout 单元格中对象的对齐方式

问题描述

我正在尝试学习如何使用 JAVA 的 GUI 库 Swing。我想要做的是在一行中放置 2 个磁盘图像,在它们下方的行中放置 3 个打印机图像。每个磁盘映像应位于两个打印机映像的中间。我(不幸的是)正在使用GridBagLayout来实现这一目标。我正在尝试构建一个 12 列的网格,并让每台打印机占用 4 个列,每个磁盘占用 6 个。然后我尝试将对象的锚点设置为,GridBagConstraints.PAGE_END使图像位于单元格的底部中间部分。无论我做什么,我都无法让磁盘正确对齐,我花了很多时间试图解决这个问题。

这是我创建对象的函数:

private void placeIcon(Container pane, GridBagConstraints c, int x, int y, String imagePath,
                               String imageLabel, int gridWidth) {
    BufferedImage printerIcon;
    try {
        printerIcon = ImageIO.read(new File(imagePath));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    JLabel textLabel = new JLabel(imageLabel + Integer.toString(x + 1),
                                SwingConstants.CENTER);
    JPanel iconPanel = new JPanel();
    iconPanel.setLayout(new GridBagLayout());
    textLabel.setPreferredSize(new Dimension(100,10));

    GridBagConstraints iconConstr = new GridBagConstraints();
    iconConstr.gridx = 0;
    iconConstr.gridy = 0;
    iconConstr.insets = new Insets(10, 3, 1, 3);
    iconConstr.anchor = GridBagConstraints.PAGE_END;
    iconConstr.weightx = 1;
    iconConstr.weighty = 1;
    iconPanel.add(textLabel, iconConstr);

    JLabel iconLabel = new JLabel(new ImageIcon(printerIcon));
    iconLabel.setPreferredSize(new Dimension(250,250));
    iconConstr.gridx = 0;
    iconConstr.gridy = 1;
    iconConstr.insets = new Insets(1, 3, 1, 3);
    iconConstr.anchor = GridBagConstraints.PAGE_END;
    iconConstr.weightx = 1;
    iconConstr.weighty = 1;
    iconPanel.add(iconLabel, iconConstr);

    c.gridx = x;
    c.gridy = y;
    c.weightx = 1;
    c.weighty = 1;
    c.anchor = GridBagConstraints.PAGE_END;
    c.gridwidth = gridWidth;
    c.insets = new Insets(25, 10, 1, 10);
    pane.add(iconPanel, c);
}

像这样调用它来创建打印机和磁盘映像:

    private void placeAllPrinterIcons(Container pane, GridBagConstraints c, int yPos) {
    String imgPath = "bin/images/printer_icon.png";
    String label = "Printer ";
    placeIcon(pane, c, 0, yPos, imgPath, label, 4);
    placeIcon(pane, c, 4, yPos, imgPath, label, 4);
    placeIcon(pane, c, 8, yPos, imgPath, label, 4);
}

private void placeAllDiskIcons(Container pane, GridBagConstraints c, int yPos) {
    String imgPath = "bin/images/disk_icon.png";
    String label = "Disk ";
    placeIcon(pane, c, 0, yPos, imgPath, label, 6);
    placeIcon(pane, c, 6, yPos, imgPath, label, 6);
}

        Container pane = getContentPane();
    pane.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    placeAllDiskIcons(pane, c, 0);
    placeAllPrinterIcons(pane, c, 1);

当前的 GUI 看起来像这样,要修复的标签: 第一个磁盘未正确对齐

标签: javaswinguser-interfacelayout-managergridbaglayout

解决方案


我正在尝试构建一个 12 列的网格,并让每台打印机占用 4 个磁盘,每个磁盘占用 6 个磁盘。

不幸的是,你不能这样做。你不能只是编列。除非每列中都有一个组件,否则布局管理器不知道列的含义。

因此,您的解决方案是嵌套具有不同布局管理器的面板。

一种方法可能是使用类似的东西:

JPanel diskPanel = new JPanel( new GridLayout(1, 0) );
diskPanel.add(disk1);
diskPanel.add(disk2);

JPanel printerPanel( new JPanel( new GridLayout(1, 0) );
printerPanel.add(printer1);
printerPanel.add(printer2);
printerPanel.add(printer3);

JPanel mainPanel = new JPanel( new GridLayout(0, 1) );
mainPanel.add(diskPanel);
mainPanel.add(printerPanel);

这将创建不同大小的网格。然后将主面板添加到框架中。

注意:如果你真的想使用有 12 列的概念,那么你需要有一排 12 个不可见的组件来占据每一列。有关此方法的示例,请查看:为什么此 GridBagLayout 未按计划显示?


推荐阅读