简体   繁体   English

如何在java的Swing GUI中将图像设置为Frame的背景?

[英]How to set an image as a background for Frame in Swing GUI of java?

I have created one GUI using Swing of Java.我使用 Java 的 Swing 创建了一个 GUI。 I have to now set one sample.jpeg image as a background to the frame on which I have put my components.How to do that ?我现在必须将一个 sample.jpeg 图像设置为我放置组件的框架的背景。怎么做?

There is no concept of a "background image" in a JPanel , so one would have to write their own way to implement such a feature. JPanel没有“背景图像”的概念,因此必须编写自己的方法来实现这样的功能。

One way to achieve this would be to override the paintComponent method to draw a background image on each time the JPanel is refreshed.实现此目的的一种方法是在每次刷新JPanel时覆盖paintComponent方法以在其上绘制背景图像。

For example, one would subclass a JPanel , and add a field to hold the background image, and override the paintComponent method:例如,可以继承一个JPanel ,并添加一个字段来保存背景图像,并覆盖paintComponent方法:

public class JPanelWithBackground extends JPanel {

  private Image backgroundImage;

  // Some code to initialize the background image.
  // Here, we use the constructor to load the image. This
  // can vary depending on the use case of the panel.
  public JPanelWithBackground(String fileName) throws IOException {
    backgroundImage = ImageIO.read(new File(fileName));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // Draw the background image.
    g.drawImage(backgroundImage, 0, 0, this);
  }
}

(Above code has not been tested.) (以上代码未经测试。)

The following code could be used to add the JPanelWithBackground into a JFrame :以下代码可用于将JPanelWithBackground添加到JFrame

JFrame f = new JFrame();
f.getContentPane().add(new JPanelWithBackground("sample.jpeg"));

In this example, the ImageIO.read(File) method was used to read in the external JPEG file.在本例中, ImageIO.read(File)方法用于读取外部 JPEG 文件。

This is easily done by replacing the frame's content pane with a JPanel which draws your image:这很容易通过用绘制图像的 JPanel 替换框架的内容窗格来完成:

try {
    final Image backgroundImage = javax.imageio.ImageIO.read(new File(...));
    setContentPane(new JPanel(new BorderLayout()) {
        @Override public void paintComponent(Graphics g) {
            g.drawImage(backgroundImage, 0, 0, null);
        }
    });
} catch (IOException e) {
    throw new RuntimeException(e);
}

This example also sets the panel's layout to BorderLayout to match the default content pane layout.此示例还将面板的布局设置为 BorderLayout 以匹配默认的内容窗格布局。

(If you have any trouble seeing the image, you might need to call setOpaque(false) on some other components so that you can see through to the background.) (如果您在查看图像时遇到任何问题,您可能需要在其他一些组件上调用setOpaque(false)以便您可以看到背景。)

背景面板条目根据您的要求显示了几种不同的方式。

Here is another quick approach without using additional panel.这是另一种不使用额外面板的快速方法。

JFrame f = new JFrame("stackoverflow") { 
  private Image backgroundImage = ImageIO.read(new File("background.jpg"));
  public void paint( Graphics g ) { 
    super.paint(g);
    g.drawImage(backgroundImage, 0, 0, null);
  }
};

if you are using netbeans you can add a jlabel to the frame and through properties change its icon to your image and remove the text.如果您使用的是 netbeans,您可以向框架添加 jlabel,并通过属性将其图标更改为您的图像并删除文本。 then move the jlabel to the bottom of the Jframe or any content pane through navigator然后通过导航器将 jlabel 移动到 Jframe 或任何内容窗格的底部

Perhaps the easiest way would be to add an image, scale it, and set it to the JFrame/JPanel (in my case JPanel) but remember to "add" it to the container only after you've added the other children components.也许最简单的方法是添加图像、缩放它并将其设置为 JFrame/JPanel(在我的情况下为 JPanel),但请记住只有在添加其他子组件后才将其“添加”到容器中。 在此处输入图片说明

    ImageIcon background=new ImageIcon("D:\\FeedbackSystem\\src\\images\\background.jpg");
    Image img=background.getImage();
    Image temp=img.getScaledInstance(500,600,Image.SCALE_SMOOTH);
    background=new ImageIcon(temp);
    JLabel back=new JLabel(background);
    back.setLayout(null);
    back.setBounds(0,0,500,600);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
  JButton b1;
  JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
 One way
-----------------*/
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);

// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads  \\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
add(l1);
add(b1);
// Just for refresh :) Not optional!
  setSize(399,399);
   setSize(400,400);
   }
   public static void main(String args[])
  {
   new BackgroundImageJFrame();
 }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM