Java 设置JFrame的背景色
如何设置JFrame的背景色?检索框架的内容窗格,并使用继承的方法更改颜色 例如:Java 设置JFrame的背景色,java,swing,graphics,awt,Java,Swing,Graphics,Awt,如何设置JFrame的背景色?检索框架的内容窗格,并使用继承的方法更改颜色 例如: myJFrame.getContentPane().setBackground( desiredColor ); 您可以使用如下容器: Container c = JFrame.getContentPane(); c.setBackground(Color.red); 当然,您必须导入java.awt.Color以获得红色颜色常量。要设置JFrame的背景色: getContentPane().setBac
myJFrame.getContentPane().setBackground( desiredColor );
您可以使用如下容器:
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
当然,您必须导入
java.awt.Color
以获得红色颜色常量。要设置JFrame的背景色:
getContentPane().setBackground(Color.YELLOW); //Whatever 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
可能最简单的方法是:
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);
}
}
super.setBackground(Color.CYAN);
在执行此操作之前,必须在类中扩展JFrame 您可以覆盖JFrame的绘制方法,然后用您喜欢的颜色填充它,如下所示:
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
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));
}
}
您好,我也遇到了同样的问题,经过多次尝试后,我发现问题在于您需要一个图形对象才能进行绘制、绘制(setBackgroundColor) 我的代码通常是这样的:
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
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)使用:
setBackground(Color.red);
不能正常工作
使用
或
要设置JFrame的背景色,请尝试以下操作:
this.getContentPane().setBackground(Color.white);
这是最简单和正确的方法。您所要做的就是在initComponents()之后添加此代码
这是一个示例RGB颜色,您可以将其替换为所需的颜色。如果您不知道RGB颜色的代码,请在网上搜索。。。有很多网站提供这样的自定义颜色。从停滞期中复活线程
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
2018年,此解决方案适用于NetBeans中的Swing/JFrame(应适用于任何IDE:):
this.getContentPane().setBackground(颜色为.GREEN)代码>我在更改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背景色
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);
与旧答案相比,没有什么新的,是吗;-)加上两个“否”:a)如果不使用b)不保留别名成员c)不使用布局管理器,则不进行扩展这可能是错误的,也可能是重复的,具体取决于您要在框架上或其内容上调用方法的位置。不仅是您给出的答案中未指定的,但这是对上面答案的重复。如果其他用户已经给出了相同的答案,为什么还要回答呢?他的第一句话很有道理,即挫折不能正常工作。我觉得这是一个解决方案的建议,可能是答案,也可能不是答案。如果OP尝试了这一点,并发现它是有效的,他们可以要求您添加作为答案来标记它。这就是我通常的工作方式。谢谢你这个伟大的解决方案。颜色中的“C”似乎是区分大小写的。一般来说,如果答案中包含对代码意图的解释,以及为什么在不介绍其他代码的情况下解决问题,那么答案会更有帮助。我用解决问题的代码具体地回答了问题。任何时候都不要像你说的那样再加一道题。@Learning。最好写:frame.getContentPane().setBackground(Color.PINK)代码>
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);