【问题标题】:How to set an image as a background for Frame in Swing GUI of java?java - 如何在Java的Swing GUI中将图像设置为Frame的背景?
【发布时间】:2009-09-23 14:11:57
【问题描述】:

我使用 Java 的 Swing 创建了一个 GUI。我现在必须将一个 sample.jpeg 图像设置为我放置组件的框架的背景。如何做到这一点?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    JPanel 中没有“背景图像”的概念,因此必须编写自己的方式来实现这样的功能。

    实现此目的的一种方法是重写paintComponent 方法,以便在每次刷新JPanel 时绘制背景图像。

    例如,可以将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 文件。

    【讨论】:

    • 这并不能完全回答问题。它将背景图像放在面板上,但随后它只是将面板插入到正常布局中。问题是如何在其他组件后面的框架上设置背景。
    • 如果你给它null作为你的ImageObserver会不会引起问题?
    【解决方案2】:

    这很容易通过将框架的内容窗格替换为绘制图像的 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),以便您可以看到背景。)

    【讨论】:

      【解决方案3】:

      Background Panel 条目会根据您的要求显示几种不同的方式。

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        也许最简单的方法是添加图像,对其进行缩放,然后将其设置为 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);
        

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

          • 我还没有发现这种技术可以正常工作。图像有时会覆盖子组件,或者有时会被正常的框架背景覆盖而不应该被覆盖。
          【解决方案7】:

          如果您使用的是 netbeans,您可以向框架添加一个 jlabel,并通过属性将其图标更改为您的图像并删除文本。然后通过导航器将jlabel移动到Jframe或任何内容窗格的底部

          【讨论】:

          • 我无法完成这项工作,因为当您添加面板时,背景图像会被推过。
          • 框架的布局必须是绝对布局。
          【解决方案8】:
          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();
           }
           }
          

          【讨论】:

            猜你喜欢
            • 2023-01-04
            • 2021-08-07
            • 2021-10-05
            • 2013-02-18
            • 2010-10-06
            • 2014-06-07
            • 2014-03-19
            • 2012-08-04
            • 1970-01-01
            相关资源
            最近更新 更多