有没有办法在public static void main中使用static(string args [])?

时间:2016-03-27 06:48:23

标签: java swing static main public

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


        }
    });   

}  

}

编译这个,我收到一条消息说我没有主要方法。如果我将main方法设为静态,我不能在我的run()中使用layoutManager,所以我想知道如何进行这个传递。或者,也许是另一种让layoutManager在这种情况下工作的方法。

3 个答案:

答案 0 :(得分:1)

你需要在主要方法中做类似的事情。

        GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();

答案 1 :(得分:1)

在java main中,method是通常启动应用程序的入口点。它与我们创建的其他方法不同,我们可以根据需要和思路提供名称,参数,返回类型。由于main方法是特殊方法,因此它具有定义的签名

签名(必填:publicstatic,返回类型:void,输入参数:String[]以及方法名称:main,全部主要字母小写字母如下

public static void main(String[] args)

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

  1. 您可以创建静态init()方法,然后从main拨打init()
  2. 您可以创建班级的对象,并调用您的方法来进一步执行您的程序。
  3. 这些只是提示,因为你可以在main中编写代码,但为了让我们的应用程序执行,尊重main方法的契约是很重要的。

答案 2 :(得分:1)

正如评论中已经说明的那样,如果没有具有该确切签名的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();
        }
    });   

 }  
}
相关问题