Java基础——解决JFrame.setBackground设置无效,mac系统IDEA编译器

本文详细介绍了在Java Swing中如何正确设置JFrame的背景颜色,包括两种有效的方法:一是使contentPane不可见并设置JFrame背景色;二是直接设置contentPane的背景色。文章通过实例代码展示了具体的实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理:

JFrame框架,一旦创建,在其中就已经包含一个内容面板。
一般我们在往JFrame中添加组件时,都加在了内容面板中,这个面板可以通过JFrame的成员方法getContentPane()取出来,所以如果设置JFrame的背景颜色,仍然会被内容面板盖住,不如设置内容面板的背景颜色,如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况。

设置方法:

方法1:得到contentPane容器,设置为不可见;否则背景颜色被遮挡,setBackground()失效
jf.getContentPane().setVisible(false);
jf.setBackground(Color.BLACK);

方法2:直接设置contentPane容器,设置颜色
jf.getContentPane().setBackground(Color.BLACK);

例子:

    private void showUI() {
        JFrame jf = new JFrame();
        jf.setName("线程小球V1");
        jf.setSize(500,500);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);

        //方法1:得到contentPane容器,设置为不可见;否则背景颜色被遮挡,setBackground()失效
        jf.getContentPane().setVisible(false);
        jf.setBackground(Color.BLACK);
        
        //方法2:直接设置contentPane容器,设置颜色
        jf.getContentPane().setBackground(Color.BLACK);

        jf.setVisible(true);

    }

 

package useless; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class UselessButFunApp extends JFrame { private JButton targetButton; private JLabel scoreLabel; private JLabel timeLabel; private Timer timer; private int score = 0; private int timeLeft = 30; private Random random = new Random(); private Dimension screenSize; public UselessButFunApp() { setTitle("随机颜色按钮挑战"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); setLayout(null); // 获取屏幕尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 创建分数标签 scoreLabel = new JLabel("分数: 0"); scoreLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); scoreLabel.setBounds(20, 20, 200, 30); add(scoreLabel); // 创建时间标签 timeLabel = new JLabel("剩余时间: 30秒"); timeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); timeLabel.setBounds(20, 60, 200, 30); add(timeLabel); // 创建目标按钮 targetButton = new JButton("点我!"); targetButton.setFont(new Font("微软雅黑", Font.BOLD, 20)); targetButton.setBounds(300, 250, 100, 60); targetButton.setOpaque(true); targetButton.setBorderPainted(false); targetButton.addActionListener(e -> { score++; scoreLabel.setText("分数: " + score); moveButton(); }); add(targetButton); // 设置初始按钮位置和颜色 moveButton(); // 创建计时器 timer = new Timer(1000, e -> { timeLeft--; timeLabel.setText("剩余时间: " + timeLeft + "秒"); if (timeLeft <= 0) { timer.stop(); targetButton.setEnabled(false); int option = JOptionPane.showConfirmDialog( this, "时间到!你的最终得分: " + score + "\n再玩一次?", "游戏结束", JOptionPane.YES_NO_OPTION ); if (option == JOptionPane.YES_OPTION) { restartGame(); } else { System.exit(0); } } }); // 添加开始按钮 JButton startButton = new JButton("开始游戏"); startButton.setFont(new Font("微软雅黑", Font.PLAIN, 18)); startButton.setBounds(320, 500, 150, 40); startButton.addActionListener(e -> { startButton.setVisible(false); timer.start(); }); add(startButton); setLocationRelativeTo(null); setVisible(true); } private void moveButton() { // 随机位置 int buttonWidth = targetButton.getWidth(); int buttonHeight = targetButton.getHeight(); int x = random.nextInt(getWidth() - buttonWidth - 20) + 10; int y = random.nextInt(getHeight() - buttonHeight - 80) + 70; // 随机颜色 Color buttonColor = new Color( random.nextInt(256), random.nextInt(256), random.nextInt(256) ); // 设置按钮位置和颜色 targetButton.setLocation(x, y); targetButton.setBackground(buttonColor); // 根据背景色调整文字颜色 int brightness = (buttonColor.getRed() * 299 + buttonColor.getGreen() * 587 + buttonColor.getBlue() * 114) / 1000; targetButton.setForeground(brightness > 128 ? Color.BLACK : Color.WHITE); } private void restartGame() { score = 0; timeLeft = 30; scoreLabel.setText("分数: 0"); timeLabel.setText("剩余时间: 30秒"); targetButton.setEnabled(true); moveButton(); timer.start(); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new UselessButFunApp()); } }纠错
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值