JFrame 类型未定义方法 setColor(Color)

The method setColor(Color) is undefined for the type JFrame

错误代码如下所示:

JFrame 类型未定义方法 setColor(Color)

package task3;

class Execute {
    
    public static void main(String[] args) throws Exception {
        
        Controller c = new Controller();    
        c.simulate();
        
    }
}

class Model {
    
    int l; //line
    int c; //column
    int[] loc = {0,0}; //location
    JFrame frame;
    int m_width;
    int m_height;
    int R = (int)(Math.random()*256);
    int G = (int)(Math.random()*256); 
    int B = (int)(Math.random()*256);
    
    public int getRectWidth() {
        return frame.getContentPane().getWidth() / c;
    }
    public int getRectHeight() {
        return frame.getContentPane().getHeight() / l;
    }
    
    
    Model(int width, int height, int line, int column, JFrame w) {
        m_width = width;
        m_height = height;
        l = line;
        c = column;
        frame = w;
    }
}
class View extends JComponent {
    
    private Model m_Mod;

    View(Model mod) {
        m_Mod = mod;
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        
        super.paintComponent(g);
        for(int i=0; i<m_Mod.l; ++i) {
            for(int j=0; j<m_Mod.c; ++j) {
                g.fillRect(m_Mod.loc[0], m_Mod.loc[1], m_Mod.getRectWidth(), m_Mod.getRectHeight());
                m_Mod.loc[0] = m_Mod.loc[0] + m_Mod.getRectWidth() + 2;
            }
            m_Mod.loc[0] = 0;
            m_Mod.loc[1] = m_Mod.loc[1] + m_Mod.getRectHeight() + 2;
        }
        m_Mod.loc[1] = 0;
    }
}
class Controller extends JFrame{
    
    private Model m_Mod;
    private View m_View;
    
    
        Controller(){
                
                JFrame window = new JFrame();
                Color color = new Color(m_Mod.R, m_Mod.G, m_Mod.B);
                m_Mod.frame.setColor(color); 

// 此处的错误代码:方法 setColor(Color) 未定义类型 JFrame


                m_Mod = new Model(500,500,3,8, window);
                m_View = new View(m_Mod);
                window.add(m_View);
                window.setSize(m_Mod.m_width,m_Mod.m_height);
                window.setLocationRelativeTo(null);
                window.setVisible(true);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
        }
        void simulate(){
            //m_View.repaint();
        }
        
}

如果我将“颜色”部分注释掉,程序将完美运行。有任何想法吗? 此外,setColor() 方法在 Java 帧 class 中,不是吗?那么为什么它不起作用? 一些网站告诉我更新我的图书馆

Furthermore the setColor() method is in the Java Frame class,no?

你读过 API 了吗?这是您尝试解决问题时要做的第一步。

您无法设置 JFrame 的颜色。您可以设置添加到框架的组件foreground/background。

通常,您会在添加到框架的面板上使用 setBackground()

m_View = new View(m_Mod);
m_View.setBackground(...); // added
window.add(m_View);

但是这不起作用,因为您正在扩展默认情况下不绘制背景的 JComponent。

如果你想扩展JComponent那么你需要添加绘画代码:

super.paintComponent( g );

g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());

g.setColor( getForeground() ); // default color for your custom painting

或者,更简单的方法是扩展 JPanel,因为 JPanel 会自动使用您设置的颜色绘制背景。

需要在paintComponent method中调用setColor() method。那是我的错误。现在可以用了,矩形是用彩色绘制的。