The reason why the graphical interface cannot be displayed after running the main program and its solution (jigsaw puzzle)

1, Preface

This problem was found when I was working on my first small project - jigsaw puzzle. After I finished the whole project, there would be a series to describe the project in detail, and another column to record some problems I encountered in the process

2, Problem description

After creating a class for the entry of the main program, no error will be reported when running, but the graphical interface is not displayed
However, if you write a program in a test class, a window will pop up (Figure 2 below)

3, Cause analysis

First, I looked at the code of the test class. There is no problem. There is no problem with the method design

import javax.swing.*;

public class Test {
    public static void main(String[] args) {

        //Create game main interface
        JFrame gameJFrame = new JFrame();
        //Set length and width
        gameJFrame.setSize(603,680);
        //Set up visualization
        gameJFrame.setVisible(true);

        //Create login interface
        JFrame loginJFrame = new JFrame();
        loginJFrame.setSize(488,430);
        loginJFrame.setVisible(true);

        //Create registration interface
        JFrame registerJFrame = new JFrame();
        registerJFrame.setSize(488,500);
        registerJFrame.setVisible(true);
    }
}

Looking at the code of each class I defined again, I found that the problem should be in the method body

import javax.swing.*;

public class GameJFrame extends JFrame{
    public void GameJFrame(){
        JFrame gameJFrame = new JFrame();
        gameJFrame.setSize(603,680);
        gameJFrame.setVisible(true);
    }
}
import javax.swing.*;

public class LoginJFrame extends JFrame {
    public void loginJFrame(){
        JFrame loginJFrame = new JFrame();
        loginJFrame.setSize(488,430);
        loginJFrame.setVisible(true);
    }
}
import javax.swing.*;

public class RegisterJFrame extends JFrame {
    public void registerJFrame(){
        JFrame registerJFrame = new JFrame();
        registerJFrame.setSize(488,500);
        registerJFrame.setVisible(true);
    }
}

  • After defining GameJFrame, LoginJFrame and RegisterJFrame methods, if you want to call the member variables in this method, you should use this keyword instead of directly calling a new object after creating a new object in the test class. In this way, since the method we call does not contain parameters, if you create a new object, it will be put into the heap memory, and the data will also be in the heap memory, so you can't access it
  • The construction method cannot have void because the main entry of the program has been defined

4, Solution

  1. Do not create a new object
  2. Use this keyword to call parameters
  3. Change the format of definition method to:
public GameJFrame(){
	//Method body
}
public LoginJFrame(){
	//Method body
}
public RegisterJFrame(){
	//Method body
}

5, Correct code demonstration

import javax.swing.*;

public class GameJFrame extends JFrame {

    public  GameJFrame(){
        this.setSize(603,680);
        this.setVisible(true);
    }
}

import javax.swing.*;

public class LoginJFrame extends JFrame {

    public LoginJFrame(){

        this.setSize(488,430);
        this.setVisible(true);
    }
}

import javax.swing.*;

public class RegisterJFrame extends JFrame {
    public  RegisterJFrame(){

        this.setSize(488,500);
        this.setVisible(true);
    }
}

6, Knowledge points involved

1. Construction method and this keyword

[Construction method and this keyword]

2. Memory principle

[Memory principle]

7, Conclusion

If you encounter other problems, you can also leave comments. For classic problems, I will also publish a separate blog

Tags: Java programming language

Posted by skalar on Thu, 08 Sep 2022 22:41:14 +0530