目前,代码是:JFrame frame = new JFrame("App");frame.setSize(1200, 800);//Give it a sizeframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on closeJPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //TU ZMIENIACframe.add(panel);//Add it to your frame(...)JPanel panelForm = new JPanel(new GridBagLayout());panel.add(panelForm);GridBagConstraints c = new GridBagConstraints();c.insets = new Insets(10,10,10,10);c.gridx = 0;c.gridy = 0;c.anchor = GridBagConstraints.LINE_END;(...)panelForm.add(label_pageCount, c);c.gridy++;try { URL url = new URL(JSONLists.thumbnail.get(page)); BufferedImage image = ImageIO.read(url); JLabel label = new JLabel(new ImageIcon(image)); panelForm.add(label);} catch (Exception exp) { exp.printStackTrace();}结果是:每个 Jlabel 都正确放置在网格上的适当位置,除了出现在右上角而不是指定位置的图像。
1 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
在这一部分中:
try {
URL url = new URL(JSONLists.thumbnail.get(page));
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
panelForm.add(label); //here
} catch (Exception exp) {
exp.printStackTrace();
}
您将组件添加到容器中,但不带GridBagConstratints. 这就是为什么该组件没有添加到正确的位置。因此,将其更改为:
panelForm.add(label,c); //Add with constraints
会修复它。
添加回答
举报
0/150
提交
取消