2014-02-23 58 views
0

我將此代碼添加到Netbeans中,並且我得到的文件test1不包含main()函數。沒有main()即使main()存在

我已經加入main()功能,但仍收到此錯誤:

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JToolBar; 
import javax.swing.KeyStroke; 

import net.miginfocom.swing.MigLayout; 

public class test1 extends JFrame { 

    // Window Vars // 
    String title; 
    int width; 
    int height; 

    // Mid Level componets // 
    JMenuBar menuBar; 
    JMenu file; 
    JToolBar toolBar; 
    JPanel map; 
    JPanel sideBar; 

    // Low Level componets // 
    JMenuItem exit; 

    JButton select; 

    public test1(String title, int width, int height) { 
     this.title = title; 
     this.width = width; 
     this.height = height; 
     this.makeInterface(); 
    } 

    public void makeInterface() { 
     // Setup JFrame 
     this.setTitle(title); 
     this.setSize(width, height); 
     this.setLocationRelativeTo(null); 
     this.setMinimumSize(new Dimension(700, 500)); 
     this.setVisible(true); 

     this.setLayout(new MigLayout(
       "debug, fillx, gap unrel rel", // Layout 
       "[grow, fill][fill]",   // Column 
       "[fill][fill]"));  // Row 
     this.makeMenu(); 
     this.addToolBars(); 
     this.makePanels(); 
     this.setupActionListeners(); 
    } 

    public void makeMenu() { 
     this.menuBar = new JMenuBar(); 
     this.file = new JMenu("File"); 
     this.file.setMnemonic(KeyEvent.VK_F); 
     this.menuBar.add(file); 

     this.exit = new JMenuItem("Exit", KeyEvent.VK_E); 
     this.exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK)); 
     this.file.add(exit); 

     this.setJMenuBar(this.menuBar); 
    } 

    public void addToolBars() { 
     this.toolBar = new JToolBar("Draggable"); 
     this.addToolBarButtons(); 
     this.add(toolBar, "span, height 20:35:50, wrap"); 
    } 

    public void addToolBarButtons() { 
     this.select = new JButton("Select"); 
     this.toolBar.add(select); 
    } 

    public void makePanels() { 
     this.map = new JPanel(); 
     this.sideBar = new JPanel(); 

     this.add(map, "width 400:600:, flowy, growy"); 
     this.add(sideBar, "width 250:300:350, flowy, growy"); 
    } 

    public void setupActionListeners() { 
     this.exit.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     }); 
    } 
    public static void main(){ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new test1("r",400,400).setVisible(true); 
      } 
     }); 
    } 
} 

可能有人請找出問題是什麼?

+0

你讀過例如請仔細閱讀http://docs.oracle.com/javase/tutorial/getStarted/application/index.html#MAIN? –

+0

我的愚蠢。謝謝!修復它 – user3344508

+0

@ user3344508如果其中任何一個幫助你,請接受答案。 – Mauren

回答

12

主要的正確的簽名是如果你使用的是一些IDE像Eclipse

+1

另外值得注意的是'String [] args'參數表示控制檯參數。 – BitNinja

2

你主要需求作爲參數(字串[] args)

0

。我建議你輸入ma並自動填充它,這樣你永遠不會犯任何錯誤。

1

必需的參數是從你的主function.The正確語法主要的聲明中缺少的是:

public static void main(String args[])