【问题标题】:Is there a way to not use static in public static void main(string args[])? [closed]有没有办法在 public static void main(string args[]) 中不使用 static? [关闭]
【发布时间】:2026-01-13 03:35:01
【问题描述】:

打包摇摆训练;

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);


        }
    });   

}  

}

编译它时,我收到一条消息说我没有 main 方法。如果我将 main 方法设为静态,则无法在 run() 中使用 layoutManager,所以我想知道如何才能通过。或者,也许是让 layoutManager 在这种情况下工作的另一种方式。

【问题讨论】:

  • “我的 run() 中不能使用 layoutManager”是什么意思?看起来对我来说应该没问题。但是是的,main 必须是静态的才能成为有效的入口点。
  • 首先创建一个具有public static void main(String[] args) 方法的“main”类,使用它来创建“main”类的一个实例。使用“主”类来实际构建和启动您的程序。作为一般经验法则,您应该避免从 JFrame 扩展,原因有很多,但因为它将您锁定在一个用例中
  • 您不能将GridBagConstraints 类型的c 转换为LayoutManager。如果您将edit 您的帖子并复制/粘贴到您收到的实际错误消息中,这将有所帮助,这会让您认为您“无法使用 LayoutManager”(而不是关于缺少 main 的消息)。
  • 仅根据您的标题,不,您无法避免 main 方法中的静态关键字。 JVM默认不知道调用哪个实例方法,所以它总是寻找main方法,由于它不知道哪个实例,所以main方法必须是静态的。
  • 顺便说一句 - 不要扩展 JFrameGridBagLayoutJPanelJButton。如果用作(标准,未修改)实例,它们中的每一个都可以正常工作。

标签: java swing static main public


【解决方案1】:

正如 cmets 中所述,不,如果没有具有该确切签名的 main 方法,您将无法执行 java 类。

public static void main(String args[])

我已经清理了一点你的代码。它仍然是您的代码,但更整洁。 每次您需要特定背景或其他任何东西时,您都不需要子类化 JPanelJButtonGridBagLayout。只需实例化原始类并使用其已定义的方法来设置其属性。

   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();
        }
    });   

 }  
}

【讨论】:

    【解决方案2】:

    在java中main方法是通常启动应用程序的入口点。它不像我们创建的其他方法,我们可以根据需要和想法提供名称、参数、返回类型。由于main 方法是特殊方法,所以它有一个定义的签名

    签名(强制:publicstatic,返回类型:void,输入参数:String[]和方法名称:main,所有字母小写)如下

    public static void main(String[] args)
    

    这是 JVM 在执行我们的应用程序期间读取的方法,我们应该在其中包含初始化代码。

    1. 您可以创建静态init() 方法,并从main 调用init()
    2. 您可以创建类的对象并调用您拥有的方法以进一步执行您的程序。

    这些只是提示,因为您可以在main 中编写代码,但是为了让我们的应用程序执行,尊重 main 方法的约定很重要。

    【讨论】:

      【解决方案3】:

      您需要在 main 方法中执行以下操作。

              GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();
      

      【讨论】: