尝试使用 java 更改背景颜色时出错

Error when trying to change the background color using java

我正在尝试 Java 中的这段代码来开始创建一个 game.I 背景颜色有问题,即使我在 [=15= 中使用了 setBackgroundColor 也默认保留] 有人告诉我如何解决这个问题吗?这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;


public class Background extends JFrame{ 

    private JButton b;
     public Background () {

         super("the title");
            setLayout(new FlowLayout());

         b=new JButton("ROLL THE DICES");

         b.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
         b.setBackground(Color.YELLOW);
         b.setBounds(20, 30, 20, 70);
         add(b);

         thehandler hand=new thehandler();
         b.addActionListener(hand);
          }
     private class thehandler implements ActionListener{
         public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(null, "Ju shtypet butonin");

     }

     }


     public static void main(String[] args) {



            Background f=new Background();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setBackground(Color.GREEN);
            f.setSize(400,300);
            f.setVisible(true);
     }}

改变

f.setBackground(Color.GREEN); 

f.getContentPane().setBackground(Color.GREEN);

您需要更改内容窗格背景。

您可以在这里阅读更多相关信息: JFrame.setBackground() not working -- why?