【问题标题】:Alignment of objects in GridBagLayout cellsGridBagLayout 单元格中对象的对齐方式
【发布时间】:2018-12-05 15:17:39
【问题描述】:

我正在尝试学习如何使用 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 看起来像这样,要修复的标签:

【问题讨论】:

    标签: java swing user-interface layout-manager gridbaglayout


    【解决方案1】:

    我正在尝试构建一个 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个不可见的组件来占据每一列。有关此方法的示例,请查看:Why does this GridBagLayout not appear as planned?

    【讨论】:

      【解决方案2】:

      试试这样的网格包布局(胶水是Box.createGlue()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-24
        • 2015-04-05
        相关资源
        最近更新 更多