当我运行代码时,它只是打开一个空窗口
我也很重要
代码的相关部分:
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构造函数,但为清楚起见显示了步骤)
创建并加载图像,然后为“标签”设置图标,如下所示
ImageIcon icon = new ImageIcon("/graphics/board.gif");
JLabel background = new JLabel();
background.setIcon(icon);
Link to ImageIcon Java Doc