2016-05-26 82 views
0

你好我目前收到我的代碼錯誤,這是錯誤'內部類中的非法靜態聲明Main.Test 修飾符'靜態'只允許在常量變量聲明'我已經在這裏搜索和我有點理解爲什麼發生這個錯誤,但我不能明確我需要改變什麼,任何幫助將不勝感激,謝謝! - 我試圖讓我的其他類的靜態,因爲我在其他一些後看到,但這仍然沒有工作非法靜態聲明

這裏是我的代碼:

public class Main extends JFrame { 
public Main() { 
    String username = System.getProperty("user.name"); 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("Projects Design"); 
    frame.setLayout(new FlowLayout()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    String text = username; 
    JTextArea textAreal = new JTextArea(text, 10, 10); 
    textAreal.setPreferredSize(new Dimension(400, 400)); 
    textAreal.setLineWrap(true); 
    textAreal.setEditable(false); 
    frame.add(textAreal); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    public class Test extends JFrame{ 
    JComboBox jc = new JComboBox(); 
    JPanel panel = new JPanel(); 
    Connection con; 
    Statement st; 
    ResultSet rs; 
    public Test() { 
     this.setSize(400, 400); 
     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     try{ 
     con = DriverManager.getConnection("Removed for obvious reasons"); 
     st = con.createStatement(); 
     String s = "SELECT Code FROM users WHERE id="12"; 
     rs = st.executeQuery(s); 
     while(rs.next()) 
     { 
      jc.addItem(rs.getString(1)); 
     } 
     }catch (Exception e) { 
     JOptionPane.showMessageDialog(null, "ERROR"); 
     }finally{ 
     try{ 
     st.close(); 
     rs.close(); 
     con.close(); 
     }catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "ERROR CLOSE"); 
     } 
     } 
     panel.add(jc); 
     this.getContentPane().add(panel); 
     this.setVisible(true); 

     } 

    public static void main(String[] args) { 
    Main rectProg = new Main(); 
    new Test(); 
    } 
+0

我已經看過這篇文章,你剛剛鏈接,這對我沒有用,所以我不知道我還能做什麼。 –

回答

0

啊我想通了,我把我的靜態類在我的內部類之外,因爲我在內部類中有一個靜態方法!感謝您的幫助