[英]Place JLabel on top of JLabel with image in
我很确定以前曾问过这个问题,但是我的情况略有不同,因为我试图将JLabel放在作为背景的JLabel上,我想使用JLabel显示变化的数字,并且数字需要显示在背景上,但是我有点摇摆n00b,在此先感谢乔纳森
Without fully appreciating your requirements, if you simply need to display text over a background image, you'd be better off placing the label on top a custom panel which is capable of painting your background. 如果不完全了解您的要求,如果只需要在背景图像上显示文本,则最好将标签放在能够绘制背景的自定义面板的顶部。
You get the benefit of a layout manager without the mess. 您可以从没有混乱的布局管理器中受益。
I'd start by having a read trough Performing Custom Painting and Graphics2D Trail . 首先,我要读一读《 执行自定义绘画和Graphics2D Trail》 。
If that seems to daunting, JLabel
is actually a type of Container
, meaning it can actually 'contain' other components. 如果这似乎令人生畏,那么JLabel
实际上是Container
,这意味着它实际上可以“包含”其他组件。
EXAMPLE 例
Background pane... 背景窗格...
public class PaintPane extends JPanel {
private Image background;
public PaintPane(Image image) {
// This is just an example, I'd prefer to use setters/getters
// and would also need to provide alignment options ;)
background = image;
}
@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Insets insets = getInsets();
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int x = (width - background.getWidth(this)) / 2;
int y = (height - background.getHeight(this)) / 2;
g.drawImage(background, x, y, this);
}
}
}
Constructed with... 用...建造
public TestLayoutOverlay() throws IOException { // Extends JFrame...
setTitle("test");
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
PaintPane pane = new PaintPane(ImageIO.read(new File("fire.jpg")));
pane.setLayout(new BorderLayout());
add(pane);
JLabel label = new JLabel("I'm on fire");
label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
label.setForeground(Color.WHITE);
label.setHorizontalAlignment(JLabel.CENTER);
pane.add(label);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
And just to show that I'm not bias ;), an example using labels... 只是为了表明我没有偏见;),使用标签的示例...
public TestLayoutOverlay() {
setTitle("test");
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel background = new JLabel(new ImageIcon("fire.jpg"));
background.setLayout(new BorderLayout());
add(background);
JLabel label = new JLabel("I'm on fire");
label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
label.setForeground(Color.WHITE);
label.setHorizontalAlignment(JLabel.CENTER);
background.add(label);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
At runtime: 在运行时:
Enjoy. 请享用。 (No full code given for copy-paste) (没有提供完整的复制粘贴代码)
you can do that by this: 您可以这样做:
JLabel l1=new JLabel();
JLabel l2=new JLabel();
l1.add(l2, JLabel.NORTH);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.