[英]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.