gpt4 book ai didi

Java JFrame - 添加标签图像

转载 作者:行者123 更新时间:2023-11-30 06:21:21 24 4
gpt4 key购买 nike

所以我试图将图像添加到我的 JFrame 中,我得到 this.我正在使用 BorderLayout.PAGE_START 但我不想要灰色背景。有没有办法“删除”该背景或使用另一个布局来获得我想要的结果?

*我还将在框架底部添加一些图像,因此我也不希望那里有灰色背景。

编辑:这是我的代码:

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
  • 考虑为需要由类的方法共享的对象创建非静态字段。或者用同样的方法获取两个图像
  • 我将我的 Swing GUI 转向制作 JPanel 以获得灵 active ,因为它们可以在需要时放置到 JFrames 或 JDialogs 或 JTabbedPanes 中,或者通过 CardLayouts 进行交换。您的窗口看起来像是一个介绍性窗口,通常是一个对话框(例如 JDialog),而不是应用程序窗口(例如 JFrame)。
  • 您想要在另一个图像之上显示一个图像,有多种方法可以实现此目的,包括在 JPanel 的 paintComponent(...) 方法重写中绘制两个图像。
  • 或者,如果您想使用 JLabels,只需为底部 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());
}
}

关于Java JFrame - 添加标签图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com