JFrame问题,打开空白

时间:2014-11-06 20:38:52

标签: jframe

对于我的一个项目(我是非常新的,甚至更新的guis)我正在制作一个基于GUI的游戏它到目前为止已经工作但是,当你完成第一级时,它重新打开第二级但是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);    
    }
}

如果我是愚蠢的并且错过了一些显而易见的东西,请不要讽刺评论,我也是这个语言和这个网站的新手。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

不要担心......没有人会给你一个讽刺的评论。这是不对的或公平的。 您的代码有很多需要改进的地方。但是祝贺你写下来!

您的问题是levelUp在第二级始终为true。这会在makingGrid()中导致无限循环。在这种情况下,JFrame停止重新绘制。