【发布时间】: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);
【问题讨论】:
标签: java swing user-interface layout-manager gridbaglayout