“Change the modifier of ‘frame’ to ‘static’ in Java”

I was told by eclipse to change the modifier of my string variable to static I don't understand why I think I'm declaring everything right, but I'm not sure

import java.awt.*;
import javax.swing.*;
public class MainClass {


    Rectangle dot1 = new Rectangle(1,1),dot2 = new Rectangle(1,1);
    JFrame frame = new JFrame("Pythagorean Theorem");


    public static void main (String[] args){

        frame.setVisible(true);
        frame.setSize(500,500);

    }


}

Solution

You define the framework as an instance variable, but use it as a static variable There are two solutions:

1) You can change the modifier of the frame to static

2) Create an instance of the class as follows:

public static void main (String[] args){
    MainClass mc = new MainClass();
    mc.frame.setVisible(true);
    mc.frame.setSize(500,500);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>