java - 如何更改 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);
}
}
解决方案
您需要通过getContentPane
更改框架的背景颜色:
frame.getContentPane().setBackground(Color.BLACK);
推荐阅读
- python - 为什么我的代码不能在列表之间来回切换?
- pytest - 如何对涉及参数化的条件进行 xfail 处理?
- ruby - 如何将数字列表与具有数字列表的另一个文件匹配?
- java - 无需任何数学运算即可使用双底数和整数指数求幂。Java中的库
- javascript - 如何在 React 上实现“跳转到内容”的 Web 可访问性功能?
- python - 求解单项和多项式 Python 类
- javascript - 如何使用 Hooks 设置状态?
- python - 尝试按数字和字母顺序对这个地址列表进行排序
- regex - 使用 spark 删除 txt 文件中的特殊字符
- java - 如何在文本文件中保留句子、行空间和段落