目前,代碼是: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();}結(jié)果是:每個 Jlabel 都正確放置在網(wǎng)格上的適當(dāng)位置,除了出現(xiàn)在右上角而不是指定位置的圖像。
1 回答

慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超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
會修復(fù)它。
添加回答
舉報
0/150
提交
取消