繁体   English   中英

将带有背景图像的JPanel添加到JFrame并将其绘制

[英]Add a JPanel with a background image to a JFrame and paint it

我正在使用GUI开发新的Java桌面应用程序,我想添加带有背景图像的JPanel 带有图像的JLabel无法使用,因为我将在背景面板顶部添加不同的标签。

所以我想出了这个例子,我想实现它。

http://www.coderanch.com/how-to/java/BackgroundImageOnJPanel

class BackgroundPanel extends JPanel
{
  Image image;
  public BackgroundPanel()
  {
    try
    {
      image = javax.imageio.ImageIO.read(getClass().getResource("Test.gif"));
    }
    catch (Exception e) { e.printStackTrace(); /*handled in paintComponent()*/ }
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g); 
    if (image != null)
      g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
  }
}    

如何在JFrame上添加和绘制面板? 我正在尝试将其添加到mainPanel但我什至不知道它是否有效。 如何调用或在哪里调用paintComponent方法?

bgPanel = new BackgroundPanel();
bgPanel.setOpaque(false);
mainPanel.add(bgPanel, new java.awt.GridBagConstraints());    

BackgroundPanel应该返回图像的大小作为最小的首选大小( @Override .. getPreferredSize() )。 如果它具有组件,则可能会更大(超级大)。

带有图像的JLabel无法使用,因为我将在背景面板顶部添加不同的标签。

有趣的是您应该提及。 可以(不一定推荐)设置JLabel的布局,然后向其添加其他JComponent对象。 在任何JPanel对象上调用setOpaque(false)很重要,否则BG图像将不会显示出来。

这说明了两种方法。

在此处输入图片说明

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class BackGroundImage {

    private JComponent ui = null;

    BackGroundImage() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridLayout(0, 1));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        try {
            BufferedImage bi1 = ImageIO.read(
                    new URL ("http://i.stack.imgur.com/OVOg3.jpg"));
            BackgroundPanel bp = new BackgroundPanel(bi1);
            ui.add(bp);
            bp.setLayout(new GridBagLayout());
            JLabel l1 = new JLabel("Using BackgroundPanel");
            Font f = l1.getFont();
            l1.setFont(f.deriveFont(32f));
            l1.setForeground(Color.RED);
            bp.add(l1);
            BufferedImage bi2 = ImageIO.read(
                    new URL ("http://i.stack.imgur.com/lxthA.jpg"));
            JLabel l = new JLabel(new ImageIcon(bi2));
            ui.add(l);
            l.setLayout(new GridBagLayout());
            JLabel l2 = new JLabel("Using JLabel");
            l2.setFont(f.deriveFont(32f));
            l2.setForeground(Color.RED);
            l.add(l2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                BackGroundImage o = new BackGroundImage();

                JFrame f = new JFrame("BackgroundPanel");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class BackgroundPanel extends JPanel {

    BufferedImage image;

    public BackgroundPanel(BufferedImage image) {
        this.image = image;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();

        int w = d.width > image.getWidth() ? d.width : image.getWidth();
        int h = d.height > image.getHeight() ? d.height : image.getHeight();

        return new Dimension(w, h);
    }
}

暂无
暂无

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

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