Netbean Java Swing,JPanel / Jlabel Cant覆盖整个框架 [英] Netbean Java Swing, JPanel/Jlabel Cant cover whole Frame
问题描述
我想让我的 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.
-
将
JPanel
拖到窗体并展开以覆盖整个框架,作为背景。
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);
}
};
您可能需要解决导入问题。只需点击 Ctrl + Shift + 我
你也会有将图像的路径更改为路径。
Also you will have to change the path of the image to your path.
这是我的文件结构
这是结果
这篇关于Netbean Java Swing,JPanel / Jlabel Cant覆盖整个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!