I am writing a program where I want to call the creation of a window in the constructor from the main method. Code:

  private class window extends JFrame { public window() { setTitle("Спасибо!"); setSize(500, 300); setVisible(true); } public void main(String[] args) { /* Вызов конструктора window */ } } 

The question is, in fact, that you need to write in place of a comment so that when you call a method, a window is created?

    1 answer 1

    Declare main method static

     import javax.swing.*; class window extends JFrame { public static void main() { new window(); } void construct() { setTitle("Hello"); setSize(200, 200); setVisible(true); } public window() { construct(); } } public class Main { public static void main(String[] args) { window.main(); } } 
    • Thanks, I do not understand what is in your method body? - WerFix
    • Declaring the variable w - Maxim Timakov
    • But why is it in this case? I just need to call the designer - WerFix
    • You can simply new window() . The variable is needed if you plan to change the parameters or transfer to other functions. - Maxim Timakov
    • I'm a little about that. When I call the method should the actions be executed in the constructor, how to do it? - WerFix