Java 如何在 JPanel (NetBeans GUI Builder) 中添加背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20888142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:02:05  来源:igfitidea点击:

How do you add Background Image in JPanel (NetBeans GUI Builder)

javaswingnetbeansjpaneljtabbedpane

提问by Sanshayan

I have JTabbedPane with five tabs and each have Jpanel i want add different images for each panel in NetBeans IDE

我的 JTabbedPane 有五个选项卡,每个选项卡都有 Jpanel 我想在 NetBeans IDE 中为每个面板添加不同的图像

采纳答案by Paul Samsotha

  1. Right click on your project and add a new package, name it resources. This will need to be done so Netbeans imports your picture into that folder
  2. Add a JLabel to the Panel
  3. Hightlight the JLabel and go to the Properties pane on the right
  4. In the property that says iconclick the ...button. That will take you to a dialog
  5. Choose External Image radio button
  6. Click the ...next to the file text field
  7. Pick your Image and click OK.
  8. Click Import to Project
  9. Click OK, You should see the image in your graphic layout
  1. 右键单击您的项目并添加一个新包,将其命名为resources. 这将需要完成,以便 Netbeans 将您的图片导入该文件夹
  2. 将 JLabel 添加到面板
  3. 突出显示 JLabel 并转到右侧的“属性”窗格
  4. 在说icon单击...按钮的属性中。这将带您进入一个对话框
  5. 选择外部图像单选按钮
  6. 单击...文件文本字段旁边的
  7. 选择您的图像并单击确定。
  8. 单击导入到项目
  9. 单击确定,您应该会在图形布局中看到图像

Note this will only give you an Image, but doesn't really act as a background, because the JLabel is it's own component. I'm not really sure how to achieve a backgroud with GUI Builder. I'm not too familiar with the technology. Though if you were to write your own code, there are numerous answers here on SO that I'm sure you'll find useful. The only tricky thing about GUI Builder is that they have auto-generated code that you really can't play around with, which circumvents what I know about creating a background image.

请注意,这只会为您提供一个图像,但实际上并不会充当背景,因为 JLabel 是它自己的组件。我不太确定如何使用 GUI Builder 实现背景。我对技术不太熟悉。尽管如果您要编写自己的代码,SO 上有很多答案,我相信您会发现它们很有用。GUI Builder 唯一棘手的事情是它们有自动生成的代码,你真的不能玩,这绕过了我所知道的关于创建背景图像的知识。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明



NOTE: this only works for JLabels as JPanels don't use Icon. An alternative would be to hand write your own JPanel code in the constructor and draw the image, overriding the paintComponentmethod.

注意:这仅适用于 JLabels,因为 JPanels 不使用 Icon。另一种方法是在构造函数中手写您自己的 JPanel 代码并绘制图像,覆盖该paintComponent方法。

回答by Nukul Goyal

  1. Change the layout of your Jframe to null.
  2. Create a jlabel and cover whole jframe with it.
  3. Add your image to the icon property of the inserted jlabel.
  4. Change the layout of jframe back to free layout. You are done It worked for me
  1. 将 Jframe 的布局更改为 null。
  2. 创建一个 jlabel 并用它覆盖整个 jframe。
  3. 将您的图像添加到插入的 jlabel 的图标属性中。
  4. 将 jframe 的布局改回自由布局。你完成了它对我有用

回答by Prabhashi

May be you'll find this link useful.

可能你会发现这个链接很有用。

This tutorial shows you how to use the IDE's GUI Builder to generate the code to include images (and other resources) in your application. In addition, you will learn how to customize the way the IDE generates image handling code.

本教程向您展示如何使用 IDE 的 GUI 生成器生成代码以在应用程序中包含图像(和其他资源)。此外,您将学习如何自定义 IDE 生成图像处理代码的方式。

Handling Images in a Java GUI Application

在 Java GUI 应用程序中处理图像

Basically, following are the steps.

基本上,以下是步骤。

  1. Drag a Label to the JFrame
  2. Add a new package (for the image to be stored)
  3. Select Label and go to the Properties category
  4. Select the Iconproperty and click 'Import to Project...'
  5. Select the image and then the newly created package
  6. In Properties window of Label, select textproperty and delete it.
  1. 将标签拖到 JFrame
  2. 添加一个新包(用于要存储的图像)
  3. 选择标签并转到属性类别
  4. 选择图标属性,然后单击“导入到项目...”
  5. 选择图像,然后选择新创建的包
  6. 在 Label 的 Properties 窗口中,选择text属性并删除它。