首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >GridBagLayout单元格中对象的对齐方式

GridBagLayout单元格中对象的对齐方式
EN

Stack Overflow用户
提问于 2018-12-05 23:17:40
回答 1查看 56关注 0票数 0

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

下面是我创建对象的函数:

代码语言:javascript
复制
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);
}

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

代码语言:javascript
复制
    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如下所示,标签需要修复:

EN

回答 1

Stack Overflow用户

发布于 2018-12-05 23:47:50

尝试这样的gridbag布局(glue为Box.createGlue())

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53635413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档