Search code examples
javaswingstaticprogram-entry-pointpublic

Is there a way to not use static in public static void main(string args[])?


package swingtraining;

import static java.awt.Color.BLACK;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.NORTH;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameTest extends JFrame{

public JFrameTest(){

    setSize(800,800);
    setTitle("Hello :D");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(true);
    setVisible(true);

}

public class GridBagLayoutTest extends GridBagLayout{

        public GridBagLayoutTest(){

        setLayout(new GridBagLayout());

        };

};

public static class JPanelTest extends JPanel{

        public JPanelTest() {

        setBackground(BLACK);
        setOpaque(true);      

    }

}          



public static class JButtonTest extends JButton{

          public JButtonTest(){



          };

        };



public void main(String args[]){

        java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
        JFrameTest T = new JFrameTest();
        JPanelTest Jp1 = new JPanelTest();
        JButtonTest Jb1 = new JButtonTest();
        GridBagLayoutTest Gb1 = new GridBagLayoutTest();
        GridBagConstraints c = new GridBagConstraints();

        c.ipadx = 100;
        c.ipady = 100;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = (new Insets(0,0,0,500));

        Jb1.setLayout((LayoutManager) c);
        T.add(Jp1);
        Jp1.add(Jb1);


        }
    });   

}  

}

Compiling this, I get a message saying I don't have a main method. If I make my main method static, I can't use layoutManager in my run(), so I was wondering how I could make this pass. Or, maybe another way of getting layoutManager to work in this instance.


Solution

  • As stated already in the comments, NO, you can't execute a java class without a main method with that exact signature.

    public static void main(String args[])
    

    I've cleanead a bit your code. It is still your code but tidier. You do not need to subclass JPanel, JButton or GridBagLayout each time that you want a particular background or whatever. Just instance the original class and use its already defined methods for setting its attributes.

       import java.awt.Color;     // no static import needed
       import java.awt.GridBagConstraints;
       import java.awt.GridBagLayout;
       import java.awt.Insets;
    
       import javax.swing.JButton;
       import javax.swing.JFrame;
       import javax.swing.JPanel;
       import javax.swing.SwingUtilities;
    
       public class JFrameTest extends JFrame {
    
           public JFrameTest() {
    
           setSize(800,800);
           setTitle("Hello :D");
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           setLocationRelativeTo(null);
           setResizable(true);
    
           initComponents();  // <- Include your components in the main frame
    
           setVisible(true);
    
      }
    
      private void initComponents() {
    
          // Use meaningful names for your variables
          // Respect Java naming conventions. No variable name start with a capital letter.         
          JPanel panel = new JPanel();
          panel.setBackground(Color.BLACK);    
          panel.setOpaque(true);
          panel.setLayout(new GridBagLayout()); // no need for static access
    
          JButton button = new JButton();
    
          GridBagConstraints gbc = new GridBagConstraints(); // this is not a Layout. It represents constrains to be used in the GribBagLayout on adding an element
          gbc.ipadx = 100;
          gbc.ipady = 100;
          gbc.gridheight = 1;
          gbc.gridwidth = 1;
          gbc.weightx = 1;
          gbc.weighty = 1;
          gbc.insets = (new Insets(0,0,0,500));
    
          panel.add(button, gbc);
          add(panel);   // <- this.add(panel) where this is your instance of JFrameTest
    
       }
    
    
     public static void main(String args[]){
    
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new JFrameTest();
            }
        });   
    
     }  
    }