2012-03-11 112 views
0

我有一個抽獎程序,曾經有很多命令和組件,等等。 但我想通過省略許多命令或組件(如按鈕或標籤)來簡化它。 當我試圖這樣做,錯誤出現(42個錯誤)所有這一切說:Java編程 - 錯誤跟蹤

error: class, interface or enum expected. 

有人可以幫我追查這些錯誤?

順便說一句,這是我的代碼(修訂版):

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

public class Case extends JFrame implements ActionListener 
{ 

    static Case app=new Case(); 
    JDesktopPane desk=new JDesktopPane(); 

    Connection cn=null; 
    ResultSet rs=null; 
    Statement stat=null; 

    JInternalFrame log=new JInternalFrame("WELCOME!", true,true,true,true); 
    JInternalFrame game=new JInternalFrame("Hi! Welcome to RAFFLE DRAW by BSIT22C"); 

    int arr[]=new int[10]; 
    int range=0; 

    JTextField gametext=new JTextField("",2); 
    JTextField txtuser=new JTextField("",20); 

    JMenuBar gamebar=new JMenuBar(); 
    JMenu mnuf=new JMenu("File"); 
    JMenuItem fnew=new JMenuItem("New Game"); 
    JMenuItem fquit=new JMenuItem("Quit Game"); 
    JMenuItem flogout=new JMenuItem("Logout"); 

    JPanel pnllog=new JPanel(); 
    JPanel pnlgame=new JPanel(); 

    JButton blog=new JButton("Login"); 
    JButton bcan=new JButton("Cancel"); 

    JButton bgen=new JButton("Generate Number Set"); 
    JButton bsubmit=new JButton("Submit Number"); 

    JCheckBox[] ck=new JCheckBox[10]; 

    JLabel lblives=new JLabel(""); 
    JLabel lbcons=new JLabel(""); 

    int life=5; 
    int userscore=0; 
    int cons=0; 


    public void init() 
    { 

     setContentPane(desk); 
     desk.add(log); 
     desk.add(game); 

     game.setVisible(false); 
     game.setSize(800,600); 
     game.setContentPane(pnlgame); 
     setJMenuBar(gamebar); 
     gamebar.add(mnuf); 
     mnuf.add(fnew); 
     mnuf.add(fscore); 
     mnuf.add(flogout); 
     mnuf.add(fquit); 

     for(int ctr=0;ctr<10;ctr++) 
     { 
      ck[ctr]=new JCheckBox(); 
     } 
     for(int ctr2=0; ctr2<10; ctr2++) 
     { 
      pnlgame.add(ck[ctr2]); 
     } 

     ck[0].setEnabled(false); 
     ck[1].setEnabled(false); 
     ck[2].setEnabled(false); 
     ck[3].setEnabled(false); 
     ck[4].setEnabled(false); 
     ck[5].setEnabled(false); 
     ck[6].setEnabled(false); 
     ck[7].setEnabled(false); 
     ck[8].setEnabled(false); 
     ck[9].setEnabled(false); 

     game.setSize(750,100); 
     pnlgame.add(gametext); 
     pnlgame.add(bsubmit); 
     pnlgame.add(bgen); 
     pnlgame.add(bup); 
     pnlgame.add(lbtimer); 
     pnlgame.add(lbtest); 
     pnlgame.add(lbscore); 
     pnlgame.add(lbachieve); 
     pnlgame.add(lblives); 
     pnlgame.add(lbcons); 
     lbachieve.setText("Bonus"); 


     bgen.addActionListener(this); 
     bsubmit.addActionListener(this); 

     setJMenuBar(gamebar); 
     gamebar.add(mnuf); 
     mnuf.add(fnew); 
     mnuf.add(fscore); 
     mnuf.add(flogout); 
     mnuf.add(fquit); 

     log.setVisible(true); 
     log.setSize(300,100); 
     log.setContentPane(pnllog); 

     pnllog.setLayout(new GridLayout(2,2)); 
     pnllog.add(blog); 
     pnllog.add(bcan); 


     setJMenuBar(gamebar); 
     gamebar.add(mnuf); 
     mnuf.add(fnew); 
     mnuf.add(fscore); 
     mnuf.add(flogout); 
     mnuf.add(fquit); 

     fnew.setEnabled(false); 
     blog.addActionListener(this); 
     bcan.addActionListener(this); 
     fnew.addActionListener(this); 
     fquit.addActionListener(this); 
     flogout.addActionListener(this); 
     app.setSize(1024,800); 
     app.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ex) 
    { 
     Object[] options={"Easy", "Normal", "Hard"}; 

     if(ex.getSource()==fnew) 
     { 
      bsubmit.setEnabled(false); 
      gametext.setEditable(false); 
      int option2=JOptionPane.showOptionDialog(null,"Choose difficulty", "New Game",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); 
      if(option2==0) 
      { 
       for(int ctrck=0; ctrck<10; ctrck++) 
       { 
        ck[ctrck].setSelected(false); 
       } 
       lbtest.setText(""); 
       gametext.setText(""); 
       lbachieve.setText(""); 
       lblives.setText(""); 
       lbcons.setText(""); 
       lbscore.setText(""); 
       life=5; 
       userscore=0; 
       bgen.setEnabled(true); 
       cons=0; 
       log.setVisible(false); 
       game.setVisible(true); 
       range=10; 
      } 
      else if(option2==1) 
      { 
       for(int ctre=0; ctre<10; ctre++) 
       { 
        ck[ctre].setSelected(false); 
       } 
       lbtest.setText(""); 
       gametext.setText(""); 
       lbscore.setText(""); 
       lbachieve.setText(""); 
       lblives.setText(""); 
       lbcons.setText(""); 
       life=5; 
       userscore=0; 
       bgen.setEnabled(true); 
       cons=0; 
       log.setVisible(false); 
       game.setVisible(true); 
       range=20; 
      } 
      else if(option2==2) 
      { 
       for(int ctrn=0; ctrn<10; ctrn++) 
       { 
        ck[ctrn].setSelected(false); 
       } 
       lbtest.setText(""); 
       gametext.setText(""); 
       lbachieve.setText(""); 
       lblives.setText(""); 
       lbcons.setText(""); 
       lbscore.setText(""); 
       life=5; 
       userscore=0; 
       bgen.setEnabled(true); 
       cons=0; 
       log.setVisible(false); 
       game.setVisible(true); 
       range=40; 
      } 

    } 
    else if(ex.getSource()==fscore) 
    { 
     { 
      int hsconf=JOptionPane.showConfirmDialog(null,"Do you want to view the current score rankings?","Please Confirm",JOptionPane.YES_NO_OPTION); 
      if(hsconf==0) 
      { 
       log.setVisible(false); 
       game.setVisible(false); 
      } 
      else 
      { 

      } 
     } 
     log.setVisible(false); 
     game.setVisible(false); 
    } 
    else if(ex.getSource()==flogout) 
    { 
     int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION); 
     if(option==0) 
     { 
      for(int q=0; q<10; q++) 
      { 
       ck[q].setSelected(false); 
      } 
      lbtest.setText(""); 
      lbachieve.setText(""); 
      lblives.setText(""); 
      lbcons.setText(""); 
      lbscore.setText(""); 
      life=5; 
      userscore=0; 
      cons=0; 
      bgen.setEnabled(true); 
     } 
     else 
     { 
      log.setVisible(true); 
     } 
     for(int e=0; e<10; e++) 
     { 
      ck[e].setSelected(false); 
     } 
     lbtest.setText(""); 
     lbachieve.setText(""); 
     lblives.setText(""); 
     lbcons.setText(""); 
     life=5; 
     userscore=0; 
     cons=0; 
     bgen.setEnabled(true); 
     log.setVisible(true); 
     game.setVisible(false); 
    } 
    else if(ex.getSource()==fquit) 
    { 
     int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION); 
     if(option==0) 
     { 
      for(int r=0; r<10; r++) 
      { 
       ck[r].setSelected(false); 
      } 
      lbtest.setText(""); 
      lbachieve.setText(""); 
      lblives.setText(""); 
      lbcons.setText(""); 
      life=5; 
      userscore=0; 
      cons=0; 
      bgen.setEnabled(true); 
      System.exit(0); 
     } 
     else 
     { 
      log.setVisible(true); 
     } 
    } 
    else if(ex.getSource()==bcan) 
    { 
     int option = JOptionPane.showConfirmDialog(null,"Do you really want to quit playing?", "Do you want to exit?",JOptionPane.YES_NO_OPTION); 
     if(option==0) 
     { 
      System.exit(0); 
     } 
     else 
     { 
      log.setVisible(true); 
     } 
    } 

    else if(ex.getSource()==bgen) 
    { 
     bsubmit.setEnabled(true); 
     gametext.setEditable(true); 
     app.generate(); 
    } 

    else if(ex.getSource()==bsubmit) 
    { 

    try 
     { 
      app.cmpans(); 
      if(ck[0].isSelected()&&ck[1].isSelected()&&ck[2].isSelected()&&ck[3].isSelected()&&ck[4].isSelected()&&ck[5].isSelected()&&ck[6].isSelected()&&ck[7].isSelected()&&ck[8].isSelected()&&ck[9].isSelected()==true) 
      { 
       int win = JOptionPane.showConfirmDialog(null,"Do you want to go for another round?","Congratulations! You WIN!",JOptionPane.YES_NO_OPTION); 
       if(win==0) 
       { 
        for(int r=0; r<10; r++) 
        { 
         ck[r].setSelected(false); 
        } 
        lbtest.setText(""); 
        life=5; 
        userscore=0; 
        gametext.setText(""); 
        lbscore.setText(""); 
        lblives.setText(""); 
        lbcons.setText(""); 
        cons=0; 
        bgen.setEnabled(true); 

        log.setVisible(false); 
        game.setVisible(true); 
       } 
       else if(win==1) 
       { 
        log.setVisible(false); 
        game.setVisible(false); 
       } 
      } 
      else if(life<=0) 
      { 
       int gameover=JOptionPane.showConfirmDialog(null,"Sorry, you lost!","GAME OVER!",JOptionPane.OK_OPTION); 
       if(gameover==0) 
       { 
        game.setVisible(false); 
        log.setVisible(false); 
       } 
       else if(gameover==1) 
       { 
        gametext.setText(""); 
        lbscore.setText(""); 
        lblives.setText(""); 
        lbcons.setText(""); 
        lbtest.setText(""); 
        lbscore.setText(""); 
        bgen.setEnabled(false); 
        bsubmit.setEnabled(false); 
        game.setVisible(false); 
        log.setVisible(false); 
       } 

      }  
     }catch(Exception ebak){} 

    } 

    else if(ex.getSource()==blog) 
    { 
     fnew.setEnabled(false); 
     bsubmit.setEnabled(false); 
     gametext.setEditable(false); 
     Object[] options1={"Easy", "Normal", "Hard"}; 
     log.setVisible(false); 
     fnew.setEnabled(true); 
     int option3=JOptionPane.showOptionDialog(null,"Choose difficulty", "New Game",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, options1[1]); 
     if(option3==0) 
     { 
      for(int ctrck2=0; ctrck2<10; ctrck2++) 
      { 
       ck[ctrck2].setSelected(false); 
      } 
      lbtest.setText(""); 
      gametext.setText(""); 
      lbachieve.setText(""); 
      lblives.setText(""); 
      lbcons.setText(""); 
      lbscore.setText(""); 
      life=5; 
      userscore=0; 
      bgen.setEnabled(true); 
      cons=0; 
      log.setVisible(false); 
      game.setVisible(true); 
      range=10; 
     } 
     else if(option3==1) 
     { 
      for(int ctre=0; ctre<10; ctre++) 
      { 
       ck[ctre].setSelected(false); 
      } 
      lbtest.setText(""); 
      gametext.setText(""); 
      lbscore.setText(""); 
      lbachieve.setText(""); 
      lblives.setText(""); 
      lbcons.setText(""); 
      life=5; 
      userscore=0; 
      bgen.setEnabled(true); 
      cons=0; 
      log.setVisible(false); 
      game.setVisible(true); 
      range=20; 
     } 
     else if(option3==2) 
     { 
      for(int ctrn=0; ctrn<10; ctrn++) 
      { 
       ck[ctrn].setSelected(false); 
      } 
      lbtest.setText(""); 
      gametext.setText(""); 
      lbachieve.setText(""); 
      lblives.setText(""); 
      lbcons.setText(""); 
      lbscore.setText(""); 
      life=5; 
      userscore=0; 
      bgen.setEnabled(true); 
      cons=0; 
      log.setVisible(false); 
      game.setVisible(true); 
      range=40; 
     } 

    } 
} 


    public void generate() 
    { 
     for(int z=0; z<=1; z++) 
     { 
      for(int i=0; i<10; i++) 
      { 
       Random rdm=new Random(); 
       arr[i] = rdm.nextInt(range)+5; 
      } 
      lbtest.setText("Random Numbers: "+arr[0]+"-"+arr[1]+"-"+arr[2]+"-"+arr[3]+"-"+arr[4]+"-"+arr[5]+"-"+arr[6]+"-"+arr[7]+"-"+arr[8]+"-"+arr[9]); 
      bgen.setEnabled(false); 
      gametext.setText(""); 
     } 

    } 

    public void cmpans() 
    { 
     boolean success=false; 
     boolean boo=false; 
     String txget; 
     txget=gametext.getText(); 
     String pars; 
     int ans; 
     pars=gametext.getText(); 
     ans=Integer.parseInt(pars); 

     for(int i=0; i<10; i++) 
     { 

      if(ans==arr[i]) 
      { 
       userscore=userscore+10; 
       lbscore.setText("Score: "+userscore); 
       ck[i].setSelected(true); 
       arr[i]=0000; 
       gametext.setText(""); 
       lblives.setText("life: "+life); 
       lbcons.setText("cons: "+cons); 
       success=true; 
      } 
      else 
      { 
       boo=true; 
      } 
     } 

     gametext.setText(""); 
     if(success==true) 
     { 
      cons++; 
      if(cons==2) 
      { 
       lbachieve.setText("You're a GOOD GUESSER!"); 
       userscore=userscore+20; 
      } 
      else if(cons==4) 
      { 
       lbachieve.setText("You're an AMAZING GUESSER!!"); 
       userscore=userscore+50; 
      } 
     } 
     else if(boo==true) 
     { 
      cons=0; 
      life--; 
      lblives.setText("life: "+life); 
      lbcons.setText("cons: "+cons); 
     } 

    } 

} 
    public static void main(String[] args) 
    { 
     app.init(); 
    } 
} 

我想不出哪一條使出現的錯誤。

+1

錯誤消息*告訴你哪些行導致錯誤。也許你應該告訴我們這些信息? – 2012-03-11 17:49:03

+1

檢查所有大括號是否匹配。 – 2012-03-11 17:49:26

回答

9

問題是你在你的巨大的(300多行!)actionPerformed方法的末尾有太多的大括號。這意味着你正在關閉你的類聲明,導致後續的錯誤。

爲了避免將來出現此問題:

  • 讓您的IDE來縮進代碼。從156行開始(在option2的聲明下面)和382行(正好在options1的聲明下面),您的縮進變得非常順利。在其他地方也很明顯。
  • 保持您的方法更多,更多更短。當一個方法開始佔用大於一個頁面的時候,你應該真的想把它重構成更小的方法。 (理想情況下,早於此 - 我喜歡小於20行的方法,儘管我沒有硬性和快速的限制。)actionPerformed之一的大小的方法是維護夢魘
+0

+1代表所有代碼 – 2012-03-11 17:53:03

+1

@JakubZaverka:複製/粘貼/編譯使其相當明確:) – 2012-03-11 18:10:44

+0

@JonSkeet_what?沒有。這是我的程序。我自己做的。所以如果你們在這裏看到一堆錯誤,那是因爲我還是一個學生。 順便說一句,我回到最後兩個錯誤.... – cryzone 2012-03-11 18:16:13