将JLabel置于JLabel之上,其中包含图像 [英] Place JLabel on top of JLabel with image in

查看:133
本文介绍了将JLabel置于JLabel之上,其中包含图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定之前已经问过这个问题,但是我的情况略有不同,因为我试图将JLabel置于JLabel作为背景的顶部,我希望使用JLabel显示更改的数字和数字需要在背景上显示,但我有点摇摆n00b,提前谢谢,Jonathan

解决方案

完全理解您的要求,如果您只需要在背景图像上显示文字,最好将标签放在能够绘制背景的自定义面板上。



你可以获得布局管理器的好处而没有乱七八糟的东西。



我从一个读取低谷开始执行自定义绘画 Graphics2D Trail



如果这似乎令人生畏, JLabel 实际上是一种 Container ,这意味着它实际上可以包含其他组件。 / p>

示例



背景窗格...

 公共类PaintPane扩展JPanel {

私有图像背景;

public PaintPane(图片图片){
//这只是一个例子,我更喜欢使用setter / getters
//并且还需要提供对齐选项;)
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);
}

}

}

使用...构建

  public TestLayoutOverlay()抛出IOException {//扩展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(我着火了);
label.setFont(label.getFont()。deriveFont(Font.BOLD,48));
label.setForeground(Color.WHITE);
label.setHorizo​​ntalAlignment(JLabel.CENTER);
pane.add(label);

pack();
setLocationRelativeTo(null);
setVisible(true);

}

只是为了表明我没有偏见;) ,使用标签的示例......

  public TestLayoutOverlay(){

setTitle(test );
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);

JLabel background = new JLabel(new ImageIcon(fire.jpg));
background.setLayout(new BorderLayout());
add(背景);

JLabel label = new JLabel(我着火了);
label.setFont(label.getFont()。deriveFont(Font.BOLD,48));
label.setForeground(Color.WHITE);
label.setHorizo​​ntalAlignment(JLabel.CENTER);
background.add(label);

pack();
setLocationRelativeTo(null);
setVisible(true);

}


I am pretty sure that this question has been asked before, but my case is slightly different as in i am trying to place a JLabel on top of a JLabel acting as a background, I want to display changing numbers using the JLabels and the numbers need to display over the background, however i am a bit of a swing n00b, thanks in advance, Jonathan

解决方案

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.

If that seems to daunting, JLabel is actually a type of Container, meaning it can actually 'contain' other components.

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);

}

这篇关于将JLabel置于JLabel之上,其中包含图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆