2016-12-17 160 views
0

這裏是我的代碼,錯誤:無法找到或加載主類FrameDemo?

package components; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/* FrameDemo.java requires no other files. */ 
public class FrameDemo { 
/** 
* Create the GUI and show it. For thread safety, 
* this method should be invoked from the 
* event-dispatching thread. 
*/ 
private static void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("FrameDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JLabel emptyLabel = new JLabel(""); 
    emptyLabel.setPreferredSize(new Dimension(175, 100)); 
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 
} 

我複製這從Oracle網站與複製和粘貼一字不差。 Here

這就是我在命令提示符下所做的事情嗎?這裏有什麼問題。我嚴肅地對待自己的結局。

Directory of C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui 

12/17/2016 12:42 PM <DIR>   . 
12/17/2016 12:42 PM <DIR>   .. 
12/17/2016 12:29 PM    2,765 FrameDemo.java 
      1 File(s)   2,765 bytes 
      2 Dir(s) 51,945,787,392 bytes free 

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui>javac Framedemo.java 

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui>java -cp . FrameDemo 
Error: Could not find or load main class FrameDemo 

C:\Users\headgearxthree\Desktop\SCRAP\JAVA\compile\gui> 

我發了一個Hello World!就在之前在其父目錄中編程,沒有任何問題。這個錯誤是什麼?出了什麼問題?這些例子應該簡單而雄辯。這是一個簡單的方法,但只要涉及圖形,所有窗口上的編程都會轉到sh * t。跆拳道?這不是重複的。所有相似的問題都參考了一個特定的實例我以無黨派的方式說過這句話,所以它可以幫助很多人。請不要鎖定。

回答

0

類是在包components這意味着

  • 它的全名是components.FrameDemo,不FrameDemo
  • 其源文件應位於名爲components的目錄中:目錄結構應與包的結構匹配。

將源文件和類文件混合在同一目錄中也是一個壞主意。你應該把你的src目錄下的源和類單獨classes目錄下:

mkdir src 
mkdir classes 
mkdir src/components 
mv FrameDemo.java src/components 
javac -d classes src/components/FrameDemo.java 
java -cp classes components.FrameDemo 

需要注意的是,如果你已經正確地閱讀教程,並從https://docs.oracle.com/javase/tutorial/uiswing/examples/components/下載整個項目zip文件,你就會有正確的結構從一開始。

+0

什麼班段嗎? –

+0

這個java風格是一種痛苦。謝謝。 –

0

編譯時的java文件名不正確。

的javac Framedemo.java

應該是

的javac FrameDemo.java

+0

其實。我只是運行del * .class並重新進行了操作,這沒有任何區別。但是,你是對的。 –

+0

剛剛得到這個工作,它並不重要! –

+0

糾正並運行後,您應該得到NoClassDefFoundError,因爲@JB Nizet在他的答案中提到了包裝物 –

相關問題