Java 在NetBeans可视化库教程中添加背景图像
我正在做一个教程,并将添加一个背景图像。我尝试过这样做:Java 在NetBeans可视化库教程中添加背景图像,java,swing,netbeans,background,Java,Swing,Netbeans,Background,我正在做一个教程,并将添加一个背景图像。我尝试过这样做: public void run() { JFrame frame = new JFrame("VisLibDemo"); try{ frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg"))))); }catch(IOException e
public void run() {
JFrame frame = new JFrame("VisLibDemo");
try{
frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
}catch(IOException e){
System.out.println("Error");
}
frame.setMinimumSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new VisLibDemo());
frame.pack();
frame.setVisible(true);
}
然而,我不明白
或者不显示背景图像,或者当背景图像显示在顶部时,其他图像将不显示
这就是我所拥有的:
我想把图像添加到背景中
我这样做:
public void run() {
ImageIcon icon = new ImageIcon(
getClass().getResource("/Images/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
JFrame frame = new JFrame("VisLibDemo");
//frame.add(new VisLibDemo());
frame.setContentPane(label);
VisLibDemo demo = new VisLibDemo();
demo.setOpaque(false); frame.add(demo);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
但我只能看到一点点背景图像(“/Images/Brg800.jpg”)
对不起,解释得不好。但是我需要一些帮助
你能帮帮我吗 将布局设置为
JLabel
JLabel label = new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
label.setLayout(new BorderLayout());
frame.setContentPane(label);
旁注 在处理将嵌入到程序中的图像时,您将希望通过资源加载,而不是通过
文件
加载,这在您正在开发的系统之外的其他系统上不起作用。按资源使用情况加载
getClass().getResource("/path/to/image.png");
您的映像应该在src
中的某个地方,在那里它将被构建到类路径中。所以对于这个文件结构
ProjectRoot
src
Pictures
Brg800.jpg
你会用这个吗
ImageIcon icon = new ImageIcon(getClass().getResource("/Pictures/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
frame.setContentPane(label);
更多信息,请参见TagWiki底部的链接
这里有一个以上提到的例子
import java.awt.*;
import javax.swing.*;
public class ASimpleExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
ImageIcon icon = new ImageIcon(
getClass().getResource("/images/stackoverflow5.png"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false);
panel.add(new JButton("Button"));
JFrame frame = new JFrame();
frame.setContentPane(label);
frame.add(panel);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
谢谢您的回复。非常明确。但是当我修改你在我身上说的代码时,图像与其余部分重叠。我不知道如何解决这个问题…将标签设置为内容窗格,同时将标签的布局设置为
BorderLayout
。然后您可以像平常一样将组件添加到框架中。我尝试了以下方法:public void run(){ImageIcon icon=new ImageIcon(getClass().getResource(“/Images/Brg800.jpg”);JLabel label=new JLabel(icon);label.setLayout(new BorderLayout());JFrame frame=new JFrame(“VisLibDemo”);frame.setContentPane(标签);frame.setSize(300300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocationByPlatform(true);frame.setVisible(true);}但是图片Brg800.jpg的反应如何:SOverlaps什么?您不需要向框架添加任何其他内容,使其与任何内容重叠。