如何在java的swing gui中将图像设置为框架的背景?

628mspwn  于 2021-09-13  发布在  Java
关注(0)|答案(8)|浏览(303)

我已经使用java的swing创建了一个gui。现在,我必须将一个sample.jpeg图像设置为放置组件的帧的背景。如何操作?

sxpgvts3

sxpgvts31#

电影中没有“背景图像”的概念 JPanel ,因此必须编写自己的方法来实现此功能。
实现这一点的一种方法是覆盖 paintComponent 方法在每次打印时绘制背景图像 JPanel 它被刷新了。
例如,一个将子类a 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);
  }
}

(以上代码尚未测试。)
以下代码可用于添加 JPanelWithBackground 变成 JFrame :

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

在本例中 ImageIO.read(File) 方法用于读取外部jpeg文件。

d7v8vwbk

d7v8vwbk2#

通过将框架的内容窗格替换为可绘制图像的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);
}

此示例还将面板的布局设置为borderlayout,以匹配默认内容窗格布局。
(如果您在查看图像时遇到任何问题,您可能需要致电 setOpaque(false) 在其他一些组件上,以便您可以看到背景。)

vc9ivgsu

vc9ivgsu3#

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

pdsfdshx

pdsfdshx4#

您可以创建组件的子类
http://www.jguru.com/faq/view.jsp?eid=9691
或者摆弄 Package 纸
http://www.java-tips.org/java-se-tips/javax.swing/wrap-a-swing-jcomponent-in-a-background-image.html

ecfdbz9o

ecfdbz9o5#

这里是另一种不使用额外面板的快速方法。

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

dohp0rv56#

如果您使用的是netbeans,则可以将jlabel添加到帧中,并通过属性将其图标更改为图像并删除文本。然后通过navigator将jlabel移动到jframe或任何内容窗格的底部

kxeu7u2r

kxeu7u2r7#

也许最简单的方法是添加一个图像,缩放它,并将其设置为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);
s71maibg

s71maibg8#

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

相关问题