【问题标题】:Java how to set JFrame background [duplicate]Java如何设置JFrame背景[重复]
【发布时间】:2022-01-22 13:58:52
【问题描述】:

我将原始代码简化为这个有问题的部分。我知道这看起来很简单,这就是为什么当它没有按我的预期工作时它让我感到困惑。它没有编译或运行时错误,但我未能将背景颜色设置为默认值。我尝试创建一个新的 Color 实例并反转 setVisible 和 setBackground 方法,这不会产生任何影响,而且我的 Google 搜索不会显示与我所做的任何不同的任何内容。

import javax.swing.JFrame;
import java.awt.Color;

public class Main extends MyFrame{
    public static void main(String[]args){
        //new MyFrame();
        JFrame frame=new JFrame("Title");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.red);
        frame.setVisible(true);
    }
}

【问题讨论】:

标签: java swing jframe


【解决方案1】:

代码:

import javax.swing.JFrame;
import java.awt.Color;

public class Main extends MyFrame {
    
    public static void main(String[]args){
        JFrame frame = new JFrame("Title");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.RED); // <-----
        frame.setVisible(true);
    }
}

结果:

【讨论】:

  • 感谢您的回答。我完全忽略了这一点。
猜你喜欢
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 2021-10-05
  • 2010-11-07
相关资源
最近更新 更多