How to set Jframe Background Image in GroupLayout Java(如何在 GroupLayout Java 中设置 Jframe 背景图像)
问题描述
我正在尝试为我的框架设置背景图像,但它不起作用.我试过这个链接:
Am trying to set a background image for my frame but it does not work. I tried this link:
在JFrame中设置背景图片
代码:
setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/Images/about.png")))));
我尝试将上述代码添加到我的 Contentpane,但它不起作用.
I tried adding the above code to my Contentpane but it does not work.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainMenu frame = new MainMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainMenu() {
setIconImage(Toolkit.getDefaultToolkit().getImage(MainMenu.class.getResource("/Images/bug-red.png")));
setTitle("Automated Bug Fixing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 712, 458);
contentPane = new JPanel();
//contentPane.setBackground(new Color(220, 220, 220));
contentPane.setForeground(new Color(32, 178, 170));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
*setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/Images/about.png")))));*
推荐答案
基本概念看起来不错.
您遇到问题的唯一可能原因是图像不存在.
The only possible reason you might be getting problems is if the image doesn't exist.
看起来您正在尝试引用应该存在于 Jar 上下文中的图像
It looks look you are trying to reference an image that should exist within the context of the Jar
代替
ImageIO.read(new File("/Images/about.png"))
试试
ImageIO.read(getClass().getResource("/Images/about.png"))
相反.
另外,不要吞下异常,确保至少记录所有异常
Also, don't swallow exceptions, make sure all exceptions are been logged at the very least
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BackgroundFrameImage {
public static void main(String[] args) {
new BackgroundFrameImage();
}
public BackgroundFrameImage() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
JLabel label = new JLabel(new ImageIcon(ImageIO.read(...))));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(label);
frame.setLayout(new BorderLayout());
JLabel text = new JLabel("Hello from the foreground");
text.setForeground(Color.WHITE);
text.setHorizontalAlignment(JLabel.CENTER);
frame.add(text);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException | HeadlessException exp) {
exp.printStackTrace();
}
}
});
}
}
这篇关于如何在 GroupLayout Java 中设置 Jframe 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 GroupLayout Java 中设置 Jframe 背景图像
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01