本文介绍了Netbean Java Swing,JPanel/Jlabel 无法覆盖整个框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 JLabel 覆盖整个框架,但是当我这样做时,它不会覆盖文本字段和确定按钮.我如何让我的 JLabel 覆盖整个框架内容.

I want to make my JLabel cover the whole frame but when i do so it wont cover the text field and ok button. how do i make my JLabel to cover whole frame content.

P/s 我想把这个 jlabel 作为背景,这样我就可以把我的图标放在 jlabel 作为背景图片.

P/s I want to make this jlabel as a background so i can put my icon in jlabel as background picture.

推荐答案

这就是我要做的.使用 JPanel 作为背景并加入一些自定义绘制代码.

Here's what I would do. Use a JPanel for the background and throw in some custom paint code.

  1. 将一个 JPanel 拖到表单上,然后将其展开以覆盖整个框架,成为背景.

  1. Drag a JPanel to the form and expand that to cover the whole frame, to be the background.

右键单击JPanel,然后从上下文菜单中选择自定义代码.您将看到以下对话框.您现在可以编辑代码.

Right click on the JPanel and select Customize Code from the context menu. You will see the following dialog. You can now edit the code.

确保从下拉菜单中选择自定义创建并输入

Make sure to select custom creation from the drop down and type this

jPanel1 = new JPanel() {
    BufferedImage img;
    {
        try {
            img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png"));
        } catch (IOException ex) {  ex.printStackTrace(); }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }
};

  • 您可能需要解决导入问题.只需点击 + +

    此外,您还必须将图像的路径更改为您的路径.

    Also you will have to change the path of the image to your path.

    这是我的文件结构

    这是结果

    这篇关于Netbean Java Swing,JPanel/Jlabel 无法覆盖整个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-23 14:02