Answer the question
In order to leave comments, you need to log in
Why move the entry point to a separate class?
While learning java, I often watch YouTube videos of other people coding and solving different problems. I noticed that some of them leave the entry point in the main class, and some take it to the launcher class. In this regard, the first question:
1. Why take out the entry point in a separate class?
Of those who do this, some initialize the program in the main function, and some create a thread in which the launcher class constructor is called. Of course, I still understand the threads, but
2. Why create a new thread if the program has its own?
I tried both and, to be honest, I do not understand what the difference is.
import javax.swing.*;
public class Launcher {
public Launcher() {
JFrame main = new JFrame("Java calculator");
main.setSize(360, 448);
main.add(new UserInterface(main));
main.setLocationRelativeTo(null);
main.setResizable(false);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setUndecorated(true);
main.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Launcher();
}
});
}
}
Answer the question
In order to leave comments, you need to log in
In short, it starts the launcher in an EDT (Event Thread Dispatch), since Swing is thread-unsafe and all GUI operations must be started on new threads to avoid freezing and other program behavior problems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question