【发布时间】:2016-03-20 02:50:04
【问题描述】:
我有一个带有以下代码的面板
public class PhotoBox extends JPanel {
private JLabel photoIcon;
private JLabel photoName;
private JLabel print;
private ImageHelper imageHelper;
public PhotoBox(File file, JLabel print) throws IOException {
imageHelper = new ImageHelper();
this.photoIcon = new JLabel();
this.photoIcon.setIcon(imageHelper.createThumbnails(file));
this.photoIcon.setMaximumSize(new Dimension(200, 150));
this.photoIcon.setAlignmentX(Component.CENTER_ALIGNMENT);
this.photoName = new JLabel();
photoName.setText((file.getName().length()) > 20 ? file.getName().substring(0,10)+".." : file.getName());
this.photoName.setAlignmentX(Component.CENTER_ALIGNMENT);
this.print = new JLabel();
this.print.setText(print.getText());
this.print.setAlignmentX(Component.CENTER_ALIGNMENT);
setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
setBorder(BorderFactory.createLineBorder(Color.black));
//setPreferredSize(new Dimension(200,150));
add(this.photoIcon);
add(this.photoName);
add(this.print);
}
}
现在我将此类面板的列表添加到另一个具有 GridLayout 的 JPanel 中
JPanel tile = new JPanel();
GridLayout layout = new GridLayout(0,4);
tile.setLayout(layout);
PhotoBox vBox = new PhotoBox(file,filePrints.get(file.getName()));
tile.add(vBox);
但我最终得到的是这样的东西
我不明白为什么我的所有 PhotoBox 对象都占用了额外的空间。我希望图像靠得很近。不要忘记,我已经尝试过 PrefSize(),但它不起作用。
使用 WrapLayout/FlowLayout
编辑:
现在我觉得问题在于PhotoBox 的BoxLayout。
【问题讨论】:
标签: java swing jpanel grid-layout