[英]Setting background color for a JFrame
您如何为 JFrame 设置背景颜色?
检索框架的内容窗格并使用继承自Component的setBackground()方法更改颜色。
例子:
myJFrame.getContentPane().setBackground( desiredColor );
为 JFrame 设置背景颜色:
getContentPane().setBackground(Color.YELLOW); //Whatever color
使用:
setBackground(Color.red);
不能正常工作。
采用
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
要么
myJFrame.getContentPane().setBackground( Color.red );
要为 JFrame 设置背景颜色,请尝试以下操作:
this.getContentPane().setBackground(Color.white);
你好,我确实遇到了同样的问题,经过多次尝试,我发现问题是你需要一个图形对象才能绘制,paint(setBackgroundColor)。
我的代码通常是这样的:
import javax.swing.*;
import java.awt.*;
public class DrawGraphics extends JFrame{
public DrawGraphics(String title) throws HeadlessException {
super(title);
InitialElements();
}
private void InitialElements(){
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// This one does not work
// getContentPane().setBackground(new Color(70, 80, 70));
}
public void paint(Graphics draw){
//Here you can perform any drawing like an oval...
draw.fillOval(40, 40, 60, 50);
getContentPane().setBackground(new Color(70,80,70));
}
}
几乎所有其他答案中缺少的部分是放置代码的位置。 那么现在你知道它进入油漆(图形G)
这是最简单和正确的方法。 您所要做的就是在 initComponents(); 之后添加此代码;
getContentPane().setBackground(new java.awt.Color(204, 166, 166));
这是一个示例 RGB 颜色,您可以将其替换为您想要的颜色。 如果您不知道 RGB 颜色的代码,请在互联网上搜索...有很多网站提供这样的自定义颜色。
您可以像这样使用容器:
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
您当然必须为红色常量导入java.awt.Color
。
这是另一种方法:
private void RenkMouseClicked(java.awt.event.MouseEvent evt) {
renk = JColorChooser.showDialog(null, "Select the background color",
renk);
Container a = this.getContentPane();
a.setBackground(renk);
}
我正在使用 netbeans ide。 对我来说, JFrame.getContentPane()
没有运行。 我用JFrame.getContentPane()
的类等效this.getContentPane
。
我在更改 JFrame 背景时也遇到了麻烦,上面的回复并没有完全解决。 我正在使用 Eclipse。 添加布局解决了这个问题。
public class SampleProgram extends JFrame {
public SampleProgram() {
setSize(400,400);
setTitle("Sample");
getContentPane().setLayout(new FlowLayout());//specify a layout manager
getContentPane().setBackground(Color.red);
setVisible(true);
}
你可以覆盖 JFrame 的paint方法,然后用你喜欢的颜色填充它,如下所示:
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
您可以将此代码块用于 JFrame 背景颜色。
JFrame frame = new JFrame("Frame BG color");
frame.setLayout(null);
frame.setSize(1000, 650);
frame.getContentPane().setBackground(new Color(5, 65, 90));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
创建一个JLabel,调整其大小,使其覆盖您的JFrame。 右键单击JLabel,查找图标,然后单击(...)按钮。 通过单击导入到项目按钮来选择图片,然后单击完成。 在“导航器”窗格中,(默认情况下,左下角如果被禁用,请转到Netbeans IDE的“ Windows”选项卡并启用它。)
使用Jlable,您还可以设置背景颜色和图像。
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
从停滞中复活一个线程。
2018 年,此解决方案适用于 NetBeans 中的 Swing/JFrame(应该适用于任何 IDE :):
this.getContentPane().setBackground(Color.GREEN);
import java.awt.*;
import javax.swing.*;
public class MySimpleLayout extends JFrame {
private Container c;
public MySimpleLayout(String str) {
super(str);
c=getContentPane();
c.setLayout(null);
c.setBackground(Color.WHITE);
}
}
frame.getContentPane().setBackground(Color.white);
可能最简单的方法是这样的:
super.setBackground(Color.CYAN);
在执行此操作之前,您必须在类中扩展 JFrame!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.