从一个面板切换到另一个面板时无法加载面板图像,文本等

时间:2016-05-20 04:20:26

标签: java

我是一名需要为我的Java课程完成游戏的高中生。我的游戏围绕着使用关卡。我在游戏中遇到的主要问题是当我装载我的一个面板时。发生的事情是屏幕僵住了。我无法按下按钮甚至关闭面板。终端没有出现错误,这是在成功编译之后。

一些信息:

  • 使用CardLayout
  • 第一个显示的面板和其他面板完美工作后。不是这个小组。
  • 当我尝试在运行时初始显示此面板时,面板为白色。
  • 使用eclipse并将SerialVersionUID添加到我的所有类中。仍然无法正常工作。

请原谅任何对你不具吸引力的代码。感谢。

class Level1 extends JPanel implements ActionListener
{
    private String [] questions;
    private String question;
    private String [] answers;
    private String answer;
    private String [] images;
    private String image;
    private String [] wrongAnswers1;
    private String wrongAnswer1;
    private String [] wrongAnswers2;
    private String wrongAnswer2;
    private String [] wrongAnswers3;
    private String wrongAnswer3;
    private int time;
    private int integer;
    private int problemNumber;
    private int lives;
    private int answerLocation;
    private int wrongOrNot;
    private int num;
    private boolean finished;
    private boolean newProblem;
    private boolean getOut;
    private boolean makeOrder;
    private JButton restartButton;
    private JButton getOutButton;
    private ButtonGroup colorgroup;
    private JRadioButton answer1;
    private JRadioButton answer2;
    private JRadioButton answer3;
    private JRadioButton answer4;
    private File file;
    private PrintWriter makesOutput;
    private Scanner lookForTwo;
    private String number;
    private boolean thereIsTwo;
    private Timer timer;
    private ArrayList<Integer> list1;
    private int i;
    private int y;

    public Level1()
    {
        questions = new String[15];
        //filled in each part of the array;
        question = "";
        answers = new String[15];
        //filled in each part of the array
        answer = "";
        images = new String[15];
        //filled in each part of the array
        image = "";
        wrongAnswers1 = new String[15];
        //filled in each part of the array
        wrongAnswer1 = "";
        wrongAnswers2 = new String[15];
        //filled in each part of the array
        wrongAnswer2 = "";
        wrongAnswers3 = new String[15];
        //filled in each part of the array
        wrongAnswer3 = "";
        time = 60;
        integer = -1;
        problemNumber = 1;
        lives = 3;
        answerLocation = -1;
        wrongOrNot = 1;
        num = 0;
        finished = false;
        newProblem = true;
        getOut = false;
        makeOrder = true;

        restartButton = new JButton("Restart"); //create buttons: Restart, Exit
        add(restartButton);    
        restartButton.addActionListener(this);

        getOutButton = new JButton("Exit");
        add(getOutButton);
        getOutButton.addActionListener(this);

        colorgroup = new ButtonGroup();

        answer1 = new JRadioButton("");//create radiobuttons to hold answer
        colorgroup.add(answer1);
        answer2 = new JRadioButton("");
        colorgroup.add(answer2);
        answer3 = new JRadioButton("");
        colorgroup.add(answer3);
        answer4 = new JRadioButton("");
        colorgroup.add(answer4);
        //radiobuttons on south side

        file = new File("SaveFiles.txt");
        makesOutput = null;    //variables for try-catch block
        lookForTwo = null;
        number = "";
        thereIsTwo = false;

        timer = new Timer(0,this);
        timer.setDelay(1000);
        timer.start();

        list1 = new ArrayList<Integer>();
        i = 0;
        y = 0;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(newProblem)    //when a new problem needs to be made, the variable needs to be true
        {
            if(makeOrder)   //create random order of problems
            {
                while(i<15) 
                {
                    list1.add(new Integer(i));
                    i++;
                }
                Collections.shuffle(list1);
                makeOrder = false;
            }

            integer++;
            /*create problems randomly along with answer based on Math.Random
            and if-else along with level #s*/

            Image img1 = Toolkit.getDefaultToolkit().getImage(images[integer]);
            g.drawImage(img1, 500, 500, this);    //make sure to have an image if needed for the problem
            question = questions[integer];
            g.drawString(question, 500, 400);
            answer = answers[integer];
            wrongAnswer1 = wrongAnswers1[integer];
            wrongAnswer2 = wrongAnswers2[integer];
            wrongAnswer3 = wrongAnswers3[integer];

            answerLocation = (int)(Math.random()*4);    //set Location of answers on radiobuttons
            if(answerLocation == 1)
            {
                answer1.setText(answer);
                answer2.setText(wrongAnswer1);
                answer3.setText(wrongAnswer3);
                answer4.setText(wrongAnswer2);
            }
            else if(answerLocation == 2)
            {
                //different locations
            }
            else if(answerLocation == 3)
            {
                //different locations
            }
            else if(answerLocation == 4)
            {
                //different locations
            }
        }
        g.drawString("Time: " + time, 900, 100);

        //time is on upperleft; use drawString and time variable
        g.drawString(problemNumber + " out of 10", 50, 50);
        if(time == 0 || lives == 0)    //if time or lives goes to zero, open GameOverScreen
            panels.show(c, "Game Over Screen1");

        if(wrongOrNot == 2)
        {
            g.setColor(Color.RED);
            g.drawString("Incorrect",600,600);
        }
        else if(wrongOrNot == 3)
        {
            g.setColor(Color.GREEN);
            g.drawString("Correct",600,600);
        }

        g.setColor(Color.BLACK);

        if(problemNumber == 6)    //try-catch block for when user completes levels
            finished = true;

        try //PrintWriter is used when 5 problems are finished 
        {    
            makesOutput = new PrintWriter("SaveFiles.txt");
            lookForTwo = new Scanner("SaveFiles.txt");
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Can't find file.");
            System.exit(1);
        }
        while(lookForTwo.hasNext())
        {
            if(number.equals("2"))
                thereIsTwo = true;
        }

        if(getOut)
        {
            panels.show(c, "Title Screen");
            getOut = false;
        }

        if(finished && thereIsTwo == false) 
            makesOutput.println("2");

        if(finished)
        {
            panels.show(c, "Level Complete");
            finished = false;
        }

        makesOutput.close();

        time--;
    }

    public void actionPerformed(ActionEvent e)
    {
        newProblem = false;
        if(answer1.isSelected() && answer1.getText() == answer)
        {
            problemNumber++;
            newProblem = true;
            wrongOrNot = 3;
            questions[integer] = "";
        }
        else if(answer2.isSelected() && answer2.getText() == answer)
        {
            //same as above
        }
        else if(answer3.isSelected() && answer3.getText() == answer)
        {
            //same as above
        }
        else if(answer4.isSelected() && answer4.getText() == answer)
        {
            //same as above
        }
        else
        {
            lives-=1;
            wrongOrNot = 2;
        }

        if(restartButton.getModel().isPressed())
        {
            problemNumber = 1;
            time = 60;
            lives = 3;
            newProblem = true;
        }
        else if(getOutButton.getModel().isPressed())
        {
            getOut = true;
        }

        repaint();
    }
}

        try //code is now from another panel that loads Level1
        {
            scanner = new Scanner(file);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Can't find file.");
            System.exit(1);
        }                
        while(scanner.hasNext())
        {
            if(word.equals("2"))
                toLevel2 = true;

            if(word.equals("3"))
                toLevel3 = true;

        }
        scanner.close();

        if(backToTitle)
        {
            panels.show(c, "Title Screen");
            backToTitle = false;
        }

        if(lvl1ButtonPressed)
            panels.show(c, "Level 1");

0 个答案:

没有答案