2014-11-06 62 views
0

对于我的一个项目(我是非常新的&甚至更​​新的guis)我正在制作一个基于GUI的游戏它迄今为止工作,但是,当你完成第一级,它重新打开级别两个,但JFrame是完全白色和没有响应,我已经尝试了一些东西,但是,但我不是很接近找出问题所在。请帮我,为游戏的代码是:问题与JFrame,打开空白

public class gameWithGuis extends JFrame implements ActionListener, Runnable{ 
    static int levelNum = 1; 
    static int find; 
    static int length = 3; 
    static int area = length * length;  
    static ArrayList<JButton> buttonsHolder = new ArrayList<JButton>(); 
    Container pane = getContentPane(); 
    static gameWithGuis threadA = new gameWithGuis(); //Starts 10 second timer 
    static Thread timerThread = new Thread(threadA); 
    static boolean levelUp = false;  

    public void run(){ 
     try{ 
      int time = 1; 
      while (time<=10){ 
       Thread.sleep(1000);     
       time++;     
      } 
      super.dispose(); 
      JOptionPane.showMessageDialog(null,"You have ran out of time! Game over!"); 
      highscores(levelNum); 
     }catch (Exception e){} 
    } 

    public void main(){   
     Scanner sc = new Scanner(System.in); 
     JOptionPane.showMessageDialog(null,"Welcome to pseudo-Where's Wally, look for the lower case L(l) character.");   
     JOptionPane.showMessageDialog(null,"You get 10 seconds to find it.");   
     JOptionPane.showMessageDialog(null,"The answer are the coordinates multiplied together.");   
     JOptionPane.showMessageDialog(null,"That is what you must type in. Good luck!");   
     JOptionPane.showMessageDialog(null,"Ready?.."); 
     char clear = (char)(12);   
     System.out.print(clear); 
     timerThread.start(); 
     makingGrid(); 
    } 

    public static void highscores(int levelNum){   
     AQAWriteTextFile2013 writer = new AQAWriteTextFile2013(); 
     AQAReadTextFile2013 reader = new AQAReadTextFile2013(); 
     Scanner scTwo = new Scanner(System.in); 
     String fileName = "highscores.txt";    
     String save = JOptionPane.showInputDialog(null,"Would you like to save your score? (yes or no): ");  
     if (save.equalsIgnoreCase("yes")){ 
      String runningLine = ""; 
      String name = JOptionPane.showInputDialog(null,"Name please: ");    
      writer.openFile(fileName, true); //opens highscore file 
      String writeLine = name + "\t" + levelNum; 
      writer.writeToTextFile(writeLine); //writes your name and the level you reached to the file 
      writer.closeFile(); 
      reader.openTextFile(fileName); //shows you the list of peoples scores 
      JOptionPane.showMessageDialog(null,"Current Highscores: "); 
      String line = reader.readLine(); 
      if (line ==null) JOptionPane.showMessageDialog(null,"- - NONE - -"); //if file is empty 
      do //prints the lines within the file 
      { 
       line = reader.readLine(); 
       runningLine = runningLine+"\n"+line+" "; 
      }while (line !=null); 
      JOptionPane.showMessageDialog(null,runningLine); 
      reader.closeFile(); 
      System.exit(0); 
     } else if (save.equalsIgnoreCase("no")){ 
      JOptionPane.showMessageDialog(null,"Okay, thank you for playing!"); 
      System.exit(0); 
     } else { 
      JOptionPane.showMessageDialog(null,"Please answer yes or no."); //validation    
     } 
    }  

    public static void main(String[] args) throws Exception{ 
     new gameWithGuis().main(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0){   

     JButton[] buttons = buttonsHolder.toArray(new JButton[buttonsHolder.size()]); 
     //   for(int i = 0; i<=amountOfButtons; i++){ 
     //    buttonsHolder[].toArray(); 
     // //    buttons[i] = buttonsHolder[i];    
     //   } 
     if(arg0.getSource().equals(buttons[find])){ 
      timerThread.stop();    
      JOptionPane.showMessageDialog(null,"Correct!");    
      levelNum++; 
      JOptionPane.showMessageDialog(null,"Level up! You are now on level: "+levelNum+". The grid has an area of "+(length*length)+" now.");    
      levelUp = true; //go through to the next level 
      pane.removeAll();   
      super.dispose(); 
      makingGrid(); 
     }else{  
      timerThread.stop(); 
      JOptionPane.showMessageDialog(null,"You guessed wrong! Game over!");    
      JOptionPane.showMessageDialog(null,"You reached level: "+levelNum+"."); 
      highscores(levelNum); 
     } 
    } 

    public void makingGrid(){ 
     do{     
      if(levelNum>1){ 
       length = length + 2; //increase length and height of box by 2 both ways  
       area = length*length; 
      } 
      JButton[] buttons = new JButton[area]; 
      Random gen = new Random(); 
      find = gen.nextInt(area);   
      setTitle("Where's J?"); 
      //    Container pane = getContentPane();    
      pane.setLayout(new GridLayout(length, length)); 
      for(int i = 0; i<area; i++){ 
       buttons[i] = new JButton(); 
       buttons[i].setText("I"); 
       if(i == find){ 
        buttons[i].setText("J"); 
       } 
       buttonsHolder.add(buttons[i]); 
       pane.add(buttons[i]); 
       buttons[i].addActionListener(this); 
      } 
      setVisible(true); 
      setDefaultCloseOperation(EXIT_ON_CLOSE);    
      //    timerThread.start();    
     } while (levelUp);  
    } 
} 

请,如果我是愚蠢和错过了一些明显没有冷言冷语过你,我是新太这个语言,这个网站。谢谢你的帮助。

回答

0

别担心...没有人会给你一个讽刺评论。这是不正确或公平的。 你的代码有很多需要改进的地方。但是,祝贺它的写作!

你的问题是levelUp在第二个层次上总是如此。这会导致makingGrid()中的无限循环。在这种情况下,JFrame停止重新绘制。

+0

谢谢你现在已经过去了!现在唯一的问题是,当你点击正确答案时,它给出的不正确? – joescull 2014-11-06 21:02:30

+0

我很高兴它帮助你。请将此标记为正确答案。你的计划有很多需要改进的地方,你会发现我相信很多其他的问题。解决它们会让你成为更好的程序员。只是一个建议:不要使用Thread.stop()。它已被弃用。这不是正确的做法。 – Chocksmith 2014-11-07 08:53:34