2011-10-04 73 views
1

中找不到,我知道這個主題已經被破壞了幾次,但是我在修復它的時候遇到了一個錯誤,這個錯誤被認爲是在其他錯誤中修復的。Java-主要方法在

public static class FlowAp extends JFrame{ 
    String one = "One"; 
    String two = "Two"; 
    String three = "Three"; 
    String four = "Four"; 
    String five = "Five"; 

public static void main(String argv[]){ 
    FlowAp fa=new FlowAp(); 
    //Change from BorderLayout default 
    fa.getContentPane().setLayout(new FlowLayout()); 
    fa.setSize(200,200); 
    fa.setVisible(true); 
} 
FlowAp(){ 

    JButton one = new JButton("One"); 
    getContentPane().add(one); 
    JButton two = new JButton("Two"); 
    getContentPane().add(two); 
    JButton three = new JButton("Three"); 
    getContentPane().add(three); 
    JButton four = new JButton("four"); 
    getContentPane().add(four); 
    JButton five = new JButton("five"); 
    getContentPane().add(five); 

} 
} 

當我居然把括號,他們看起來好像他們是應該的,另一個錯誤顯示了與flowap「無效的方法聲明」

+0

*其中*是無效的方法聲明,完全是?這是另一個嵌套類嗎?請給*所有*你看到的錯誤的細節。 –

+0

你如何嘗試啓動程序? – ShiDoiSi

+1

問題標題似乎不匹配身體。 –

回答

1

應該是:

public static void main(String[] argv){ 

發佈當你寫這個時發生的錯誤。

注意:
- 一個類不能是靜態的。

+0

沒有什麼區別,'[]'s通常首選你的方式,但C兼容的方式。 –

+0

在java中它並不重要[]是String []等於是相同的作爲字符串等等[]; – LordDoskias

+1

我不明白在這裏upvotes ... – mre

2

在你的情況下不允許你的類的修飾符「static」 - 刪除它,它將起作用。如果你想訪問你的變量,你必須使它們成爲靜態的,以便你可以從主要方法中引用它們。

+0

我不這麼認爲,但它是說,沒有靜態,這是沒有辦法,它給出了一個單獨的錯誤信息,關於你如何不能引用一個非靜態變量可以在靜態環境中使用。 –

+0

然後,你應該讓你的變量是靜態的,而不是你的類 – LordDoskias

+0

感謝您的提示。 –

2

嘗試刪除 「靜態」:

public class FlowAp extends JFrame{ 
+0

當我從類中移除靜態時,它聲明我不能在靜態上下文中引用非靜態變量。 –

2

there是基本的東西,也有很多錯誤,我不能評論什麼的,然後

enter image description here

從代碼

import java.awt.FlowLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class FlowAp extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private String one = "One"; 
    private String two = "Two"; 
    private String three = "Three"; 
    private String four = "Four"; 
    private String five = "Five"; 

    public FlowAp() { 
     JButton oneButton = new JButton(one); 
     JButton twoButton = new JButton(two); 
     JButton threeButton = new JButton(three); 
     JButton fourButton = new JButton(four); 
     JButton fiveButton = new JButton(five); 

     setTitle("FlowAp"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     add(oneButton); 
     add(twoButton); 
     add(threeButton); 
     add(fourButton); 
     add(fiveButton); 
     setLocation(100, 100); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String argv[]) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       FlowAp fa = new FlowAp(); 
      } 
     }); 
    } 
} 
+0

感謝您的幫助。 –