java - GridBagLayout 单元格中对象的对齐方式

标签 java swing user-interface layout-manager gridbaglayout

我正在尝试学习如何使用JAVA的GUI库Swing。我想做的是将 2 个磁盘图像排成一行,并将 3 个打印机图像放在它们下面的行中。每个磁盘镜像应位于两个打印机镜像的中间。我(不幸的是)正在使用 GridBagLayout为了达成这个。我正在尝试构建一个 12 列的网格,并让每台打印机占用 4 个列,每个磁盘占用 6 个列。然后,我尝试将对象的 anchor 设置为 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 如下所示,标签待修复: First disk not aligned properly

最佳答案

I am trying to construct a grid of 12 columns and have each printer take 4 and each disk 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 个不可见组件来占据每一列。有关此方法的示例,请查看:Why does this GridBagLayout not appear as planned?

关于java - GridBagLayout 单元格中对象的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635413/

相关文章:

java - 如何修复位于另一个预先绘制的 JPanel 之上的 JPanel 上的动态移动图像的闪烁?

使用多个 UIGestureRecognizers 关闭 iOS 键盘

c# - 泛型、协变/逆变等

java - 如何在 JTextPane 中插入 html 特殊字符

java - For循环如果里面不工作

java - 在java中加载大文本文件的最佳方式

java - 如何使 for 循环在每圈之间等待 1 秒?

Java Swing 定时器不清晰

java - 无法从内部 Action 到达现场

java - 是否需要在 Swing 的 UI Thread 上更新 UI?