I've been struggling with the same issue and I think I found a solution to this that seems to be working consistently (haven't tested this more than 2 minutes, but it seems fine). Found it here https://www.javaprogrammingforums.com/awt-java-swing/4183-swing-components-invisible-startup.html
But basically: I create the JFrame as part of the class attributes (JFrame visibility set false) - then set the components visibility after adding them to the JFrame - then I set the Visibility of the JFrame to true after everything, or in a different method ---
class Main {
public static JFrame Login;
public static void main(String[] args) {
Create_Frames();
Login();
}
public static void Create_Frames() {
// === Login page GUI creation ===
Login = new JFrame("Login");
Login.setSize(400, 200);
Login.setLayout(null);
Login.setVisible(false);
Login.setResizable(false);
Login.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel Name_label = new JLabel("Enter your username:");
Name_label.setBounds(10, 10, 130, 20);
//....Other code....
Login.add(Name_label);
Login.add(Name_input);
Login.add(Pass_label);
Login.add(Pass_input);
Login.add(Login_but);
Login.add(Register_but);
Name_label.setVisible(true);
Pass_label.setVisible(true);
Name_input.setVisible(true);
Pass_input.setVisible(true);
Login_but.setVisible(true);
Register_but.setVisible(true);
}
// Other method to set frame to true
public static void Login() {
Login.setVisible(true);
}
If this worked for you please let me know