如何更改 JFrame 背景颜色

通常,要更改 JFrame 的背景,您只需调用 JFrame 方法 setBackground(Color c)

frame.setBackground(颜色.BLUE);

JFrame 包含更多技巧,您可以将其应用于 java 颜色类

  • RGB values
  • 使用更亮、 更深或更亮

还有其他方法可以检索组件的颜色等。

JFrame 示例 setBackground(color) 

您可以在 IDE 中测试此代码:

import java.awt.Color;
导入 java.awt.Dimension;
导入 javax.swing.JFrame;

public class Test extends JFrame{

public static void main(String[] args) {

JFrame frame = new JFrame(JFrame test”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

frame.setPreferredSize(new Dimension(400, 300));
frame.getContentPane().setBackground(Color.ORANGE);
frame.pack();
frame.setVisible(true);
}
}
更改