【发布时间】:2016-06-16 14:21:55
【问题描述】:
我有一个 Java Swing 应用程序。我想插入一个以 png 图像为背景的 JLabel,并且我想在该图像的中心插入另一个 JLabel。所以我已经构建了这段代码:
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/cornometro_white.png"));
labelSfondoTimer = new JLabel(icon);
labelTempoGara = new JLabel();
labelTempoGara.text("pippo");
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setComponentOrientation(ComponentOrientation.UNKNOWN);
CR2.setLayout(GBL2);
labelSfondoTimer.add(CR2);
GBC2 = new GridBagConstraints();
CR2.add(labelTempoGara);
GBC2.gridx=2;
GBC2.gridy=0;
GBC2.anchor= GridBagConstraints.CENTER;
GBL2.setConstraints(labelTempoGara,GBC2);
使用此代码,我可以在屏幕上看到图像,但看不到带有文本“pippo”的第二个标签
我也尝试插入此代码:
labelSfondoTimer.setObaque(true); 但不起作用。
【问题讨论】:
-
JLabel API中没有LayoutManager,JLabel有居中方法,JLabel默认透明,GBC默认JComponent居中无约束
-
那么,我该如何解决我的错误?
-
@bircastri 看看这是否有帮助:Image which overlaps
标签: java swing jlabel centering