Java JFrame后台不工作。仅在移动实际窗口时工作

Java JFrame后台不工作。仅在移动实际窗口时工作,java,swing,jframe,Java,Swing,Jframe,我在设置图像作为背景时遇到一些问题。仅当我移动整个窗口时,图像才会显示。下面是我的代码片段,包括设置背景图像 playButton = new JButton(pbI); instructionButton = new JButton(ibI); quitButton = new JButton(qbI); mainFrame = new JFrame("My Major Project"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_

我在设置图像作为背景时遇到一些问题。仅当我移动整个窗口时,图像才会显示。下面是我的代码片段,包括设置背景图像

playButton = new JButton(pbI);
instructionButton = new JButton(ibI);
quitButton = new JButton(qbI);

mainFrame = new JFrame("My Major Project");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 600);
mainFrame.setLocationRelativeTo(null);
mainFrame.setLayout(null);

mainFrame.setLayout(new BorderLayout());
mainFrame.setContentPane(new JLabel(new ImageIcon("background.gif")));
mainFrame.setLayout(new FlowLayout());

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

为了更新
JFrame
,需要调用
mainFrame.repaint() > 

请忽略第一行代码,因为它们是图像ButoSaveIt,使用<代码> IMAGICONION/CODE >,它可能有问题,请考虑使用<代码> IMAIOO 。为此目的使用
JLabel
并不是最好的主意,因为
JLabel
只会根据图标和文本属性计算其首选大小,这意味着它可能会裁剪其内容。你可以考虑使用类似的东西,它可以控制面板的<代码>首选大小> /代码>谢谢。