Java JFrame - 添加标签图像
Java JFrame - Add label-image
因此,我正在尝试将图像添加到我的 JFrame,但我得到 我正在使用 BorderLayout.PAGE_START,但我不想要灰色背景。有没有办法 "remove" 该背景或使用另一个布局来获得我想要的结果?
*我还将在相框的底部添加一些图像,这样我也不希望那里也有灰色背景。
编辑:
这是我的代码:
private JFrame getCreatedFrame(){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(screenSize);
frame.setLocationRelativeTo(null);
JFrame.setDefaultLookAndFeelDecorated(true);
//set icon image
String imgName = "images/domino.png";
URL imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
frame.setIconImage(icon.getImage());
//set background image
imgName = "images/background.jpg";
imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
JLabel background=new JLabel(icon);
frame.add(background);
return frame;
}
public void start() {
short version=0,choice=0;
JFrame frame=getCreatedFrame();
//set welcome image
String imgName = "images/welcome.png";
URL imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
JLabel welcome=new JLabel(icon);
frame.add(welcome,BorderLayout.PAGE_START);
frame.setVisible(true);
}
建议:
- 使您的代码更符合 OOP
- 考虑为需要由 class 的方法共享的对象创建非静态字段。或者以相同的方法获取两个图像
- 我将我的 Swing GUI 用于制作 JPanels 以获得灵活性,因为它们可以放置到 JFrames 或 JDialogs 或 JTabbedPanes 中,或者通过 CardLayouts 在任何需要的地方进行交换。您的 window 看起来像是一个介绍性的 window,它通常是一个对话框(例如 JDialog)而不是一个应用程序 window(例如 JFrame)。
- 您想在另一个图像之上显示一个图像,有多种方法可以做到这一点,包括在 JPanel 的
paintComponent(...)
方法覆盖中绘制两个图像。
- 或者,如果您想使用 JLabel,只需给底部的 JLabel 一个布局管理器,例如 FlowLayout,然后将较小的 JLabel 添加到底部。
例如:
import java.awt.FlowLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class WelcomePanel extends JPanel {
public WelcomePanel(Image backGround, Image foreGround) {
JLabel backGroundLabel = new JLabel(new ImageIcon(backGround));
JLabel foreGroundLabel = new JLabel(new ImageIcon(foreGround));
// if you want the welcome image away from the edge, then give
// backGroundLabel an empty border with appropriate insets. For example:
// backGroundLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
backGroundLabel.setLayout(new FlowLayout());
backGroundLabel.add(foreGroundLabel);
}
private static void createAndShowGui() {
String backImgName = "images/domino.png";
String foreImgName = "images/welcome.png";
URL backImageURL = WelcomePanel.class.getResource(backImgName);
URL foreImageURL = WelcomePanel.class.getResource(foreImgName);
Image backGroundImg = null;
Image foreGroundImg = null;
if (backImageURL != null && foreImageURL != null) {
try {
backGroundImg = ImageIO.read(backImageURL);
foreGroundImg = ImageIO.read(foreImageURL);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
WelcomePanel mainPanel = new WelcomePanel(backGroundImg, foreGroundImg);
// or perhaps better to use a JDialog
JFrame frame = new JFrame("Image On Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
因此,我正在尝试将图像添加到我的 JFrame,但我得到
*我还将在相框的底部添加一些图像,这样我也不希望那里也有灰色背景。
编辑: 这是我的代码:
private JFrame getCreatedFrame(){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(screenSize);
frame.setLocationRelativeTo(null);
JFrame.setDefaultLookAndFeelDecorated(true);
//set icon image
String imgName = "images/domino.png";
URL imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
frame.setIconImage(icon.getImage());
//set background image
imgName = "images/background.jpg";
imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
JLabel background=new JLabel(icon);
frame.add(background);
return frame;
}
public void start() {
short version=0,choice=0;
JFrame frame=getCreatedFrame();
//set welcome image
String imgName = "images/welcome.png";
URL imageURL = getClass().getResource(imgName);
if (imageURL != null) {
icon = new ImageIcon(imageURL);
}
JLabel welcome=new JLabel(icon);
frame.add(welcome,BorderLayout.PAGE_START);
frame.setVisible(true);
}
建议:
- 使您的代码更符合 OOP
- 考虑为需要由 class 的方法共享的对象创建非静态字段。或者以相同的方法获取两个图像
- 我将我的 Swing GUI 用于制作 JPanels 以获得灵活性,因为它们可以放置到 JFrames 或 JDialogs 或 JTabbedPanes 中,或者通过 CardLayouts 在任何需要的地方进行交换。您的 window 看起来像是一个介绍性的 window,它通常是一个对话框(例如 JDialog)而不是一个应用程序 window(例如 JFrame)。
- 您想在另一个图像之上显示一个图像,有多种方法可以做到这一点,包括在 JPanel 的
paintComponent(...)
方法覆盖中绘制两个图像。 - 或者,如果您想使用 JLabel,只需给底部的 JLabel 一个布局管理器,例如 FlowLayout,然后将较小的 JLabel 添加到底部。
例如:
import java.awt.FlowLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class WelcomePanel extends JPanel {
public WelcomePanel(Image backGround, Image foreGround) {
JLabel backGroundLabel = new JLabel(new ImageIcon(backGround));
JLabel foreGroundLabel = new JLabel(new ImageIcon(foreGround));
// if you want the welcome image away from the edge, then give
// backGroundLabel an empty border with appropriate insets. For example:
// backGroundLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
backGroundLabel.setLayout(new FlowLayout());
backGroundLabel.add(foreGroundLabel);
}
private static void createAndShowGui() {
String backImgName = "images/domino.png";
String foreImgName = "images/welcome.png";
URL backImageURL = WelcomePanel.class.getResource(backImgName);
URL foreImageURL = WelcomePanel.class.getResource(foreImgName);
Image backGroundImg = null;
Image foreGroundImg = null;
if (backImageURL != null && foreImageURL != null) {
try {
backGroundImg = ImageIO.read(backImageURL);
foreGroundImg = ImageIO.read(foreImageURL);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
WelcomePanel mainPanel = new WelcomePanel(backGroundImg, foreGroundImg);
// or perhaps better to use a JDialog
JFrame frame = new JFrame("Image On Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}