2016-05-26 83 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

啊我想通了,我把我的静态类在我的内部类之外,因为我在内部类中有一个静态方法!感谢您的帮助