gpt4 book ai didi

java - 使用 JLabel 作为背景图像

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

当我运行代码时,它只会打开一个空窗口我也很重视任何必要的事情

代码的相关部分:

public class Game extends JFrame implements ActionListener,KeyListener{
private JLabel background;
....
public Game(){
background=new JLabel(new ImageIcon("/graphics/board.gif"));
...
this.add(background);
this.setSize(800,600);
this.setVisible(true);...

我尝试将 JLabel 添加到 JPanel,然后将其添加到框架,但窗口中仍然不显示任何内容

最佳答案

最初的代码是:

JLabel background = new JLabel("/graphics/board.gif");

这不会在所描述的路径处设置图像,建议使用以下方法(这可以简化为仅使用不同的 JLabel 构造函数,但为清楚起见显示了步骤)

创建并加载图片,然后为Label设置图标如下

ImageIcon icon = new ImageIcon("/graphics/board.gif"); 
JLabel background = new JLabel();
background.setIcon(icon);

Link to ImageIcon Java Doc

关于java - 使用 JLabel 作为背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265075/

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