【问题标题】:Illegal static declaration [duplicate]非法静态声明 [重复]
【发布时间】:2016-09-24 03:00:40
【问题描述】:

您好,我的代码当前收到错误,这是错误“内部类 Main.Test 中的非法静态声明” 修饰符 'static' 只允许在常量变量声明中使用'我在这里搜索过,我有点理解为什么会发生这个错误,但我无法具体弄清楚我需要更改什么,任何帮助将不胜感激,谢谢! - 正如我在其他帖子中看到的那样,我试图让我的其他课程保持静态,但这仍然不起作用

这是我的代码:

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();
    }

【问题讨论】:

  • 是的,我已经看过你刚刚链接的这篇文章,但这对我不起作用,所以我不确定我还能做什么。

标签: java swing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多