首页 > 解决方案 > 如何更改 JFrame 的背景颜色?

问题描述

我试图改变 a 的背景颜色,JFrame但它从来没有用过。这是我的代码:

package com.company;

import javax.swing.*;
import java.awt.*;

public class Main extends JPanel
{
    private static JFrame frame = new JFrame();
    private static int rand = 3;

    public static void main(String[] args) 
    {
        frame.getContentPane().add(new Main());
        frame.setSize(1960, 1070);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}

标签: javagraphics

解决方案


您需要通过getContentPane更改框架的背景颜色:

frame.getContentPane().setBackground(Color.BLACK);

推荐阅读