【发布时间】:2009-09-23 14:11:57
【问题描述】:
我使用 Java 的 Swing 创建了一个 GUI。我现在必须将一个 sample.jpeg 图像设置为我放置组件的框架的背景。如何做到这一点?
【问题讨论】:
我使用 Java 的 Swing 创建了一个 GUI。我现在必须将一个 sample.jpeg 图像设置为我放置组件的框架的背景。如何做到这一点?
【问题讨论】:
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会不会引起问题?
这很容易通过将框架的内容窗格替换为绘制图像的 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),以便您可以看到背景。)
【讨论】:
Background Panel 条目会根据您的要求显示几种不同的方式。
【讨论】:
也许最简单的方法是添加图像,对其进行缩放,然后将其设置为 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);
【讨论】:
这是另一种不使用额外面板的快速方法。
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);
}
};
【讨论】:
如果您使用的是 netbeans,您可以向框架添加一个 jlabel,并通过属性将其图标更改为您的图像并删除文本。然后通过导航器将jlabel移动到Jframe或任何内容窗格的底部
【讨论】:
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();
}
}
【讨论】: