1

I'm trying to make something for fun and my components keep disappearing after I press the "ok" button in my gui.

I'm trying to make a "Guess a word" program, where one will get a tip and you can then enter a guess and if it matches it will give you a message and if not, another tip. The problem is, if you enter something that's not the word it will give you a message that it's not the correct word and it will then give you another tip. But the other tip won't show up. They disappear.

I've two classes, "StartUp" and "QuizLogic". The problem arrives somewhere in the "showTips" method (I think).

If someone would try it themselves a link to the file can be found here: Link to file

Thank you.

public class StartUp {

    private JFrame frame;

    private JButton showRulesYesButton;
    private JButton showRulesNoButton;
    private JButton checkButton;

    private JPanel mainBackgroundManager;

    private JTextField guessEntery;

    private QuizLogic quizLogic;

    private ArrayList<String> tips;

    private JLabel firstTipLabel;
    private JLabel secondTipLabel;
    private JLabel thirdTipLabel;

    // CONSTRUCTOR
    public StartUp() {
        // Show "Start Up" message 
        JOptionPane.showMessageDialog(null, "Guess a Word!");
        showRules();
    }

    // METHODS
    public void showRules() {
        // Basic frame methods
        frame = new JFrame("Rules");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(500, 500));
        frame.setLocationRelativeTo(null);

        // Creates JLabels and adds to JPanel
        JLabel firstRule = new JLabel("1. You will get one tip at a time of what the word could be.");
        JLabel secoundRule = new JLabel("2. You will get three tips and unlimited guesses.");
        JLabel thirdRule = new JLabel("3. Every word is a noun and in its basic form.");
        JLabel understand = new JLabel("Are you ready?");

        // Creates JPanel and adds JLabels to JPanel
        JPanel temporaryRulesPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 100, 60));

        temporaryRulesPanel.add(firstRule);
        temporaryRulesPanel.add(secoundRule);
        temporaryRulesPanel.add(thirdRule);
        temporaryRulesPanel.add(understand);

        // Initialize buttons
        showRulesYesButton = new JButton("Yes");
        showRulesNoButton = new JButton("No");

        showRulesYesButton.addActionListener(new StartUpEventHandler());
        showRulesNoButton.addActionListener(new StartUpEventHandler());

        // Creates JPanel and adds button to JPanel
        JPanel temporaryButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 100, 35));

        temporaryButtonPanel.add(showRulesYesButton);
        temporaryButtonPanel.add(showRulesNoButton);

        // Initialize and adds JPanel to JPanel
        mainBackgroundManager = new JPanel(new BorderLayout());

        mainBackgroundManager.add(temporaryRulesPanel, BorderLayout.CENTER);
        mainBackgroundManager.add(temporaryButtonPanel, BorderLayout.SOUTH);

        //Adds JPanel to JFrame
        frame.add(mainBackgroundManager);
        frame.setVisible(true);
    }

    public void clearBackground() {
        mainBackgroundManager.removeAll();

        quizLogic = new QuizLogic();
        showGuessFrame();
        showTips();
    }

    public void showGuessFrame() {
        JPanel guessPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 10));

        firstTipLabel = new JLabel("a");
        secondTipLabel = new JLabel("b");
        thirdTipLabel = new JLabel("c");

        tips = new ArrayList<String>();

        guessEntery = new JTextField("Enter guess here", 20);

        checkButton = new JButton("Ok");
        checkButton.addActionListener(new StartUpEventHandler());

        guessPanel.add(guessEntery);
        guessPanel.add(checkButton);

        mainBackgroundManager.add(guessPanel, BorderLayout.SOUTH);

        frame.add(mainBackgroundManager);
    }

    public void showTips() {
        JPanel temporaryTipsPanel = new JPanel(new GridLayout(3, 1));

        temporaryTipsPanel.removeAll();

        tips.add(quizLogic.getTip());

        System.out.println(tips.size());

        if (tips.size() == 1) {
            firstTipLabel.setText(tips.get(0));
        }
        if (tips.size() == 2) {
            secondTipLabel.setText(tips.get(1));
        }
        if (tips.size() == 3) {
            thirdTipLabel.setText(tips.get(2));
        }

        temporaryTipsPanel.add(firstTipLabel);
        temporaryTipsPanel.add(secondTipLabel);
        temporaryTipsPanel.add(thirdTipLabel);

        mainBackgroundManager.add(temporaryTipsPanel, BorderLayout.CENTER);

        frame.add(mainBackgroundManager);
        frame.revalidate();
        frame.repaint();
    }

    public void getGuess() {
        String temp = guessEntery.getText();

        boolean correctAnswer = quizLogic.checkGuess(guessEntery.getText());

        if (correctAnswer == true) {
            JOptionPane.showMessageDialog(null, "Good Job! The word was " + temp);
        }
        else {
            JOptionPane.showMessageDialog(null, temp + " is not the word we are looking for");
            showTips();
        }
    }

    private class StartUpEventHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == showRulesYesButton) {
                clearBackground();
            }
            else if (event.getSource() == showRulesNoButton) {
                JOptionPane.showMessageDialog(null, "Read the rules again");
            }
            else if (event.getSource() == checkButton) {
                getGuess();
            }
        }
    }
}


public class QuizLogic {

    private ArrayList<String> quizWord;
    private ArrayList<String> tipsList;

    private int tipsNumber;

    private String word;

    public QuizLogic() {
        tipsNumber = 0;

        quizWord = new ArrayList<String>();
        quizWord.add("Burger");

        try {
            loadTips(getWord());
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public String getWord() {
        Random randomGen = new Random();        
        return quizWord.get(randomGen.nextInt(quizWord.size()));
    }

    public void loadTips(String word) throws FileNotFoundException {
        Scanner lineScanner = new Scanner(new File("C:\\Users\\Oliver Nielsen\\Dropbox\\EclipseWorkspaces\\BuildingJava\\GuessAWord\\src\\domain\\" + word + ".txt"));

        this.word = word;
        tipsList = new ArrayList<String>();

        while (lineScanner.hasNext()) {
            tipsList.add(lineScanner.nextLine());
        }
    }

    public String getTip() {
        if (tipsNumber >= tipsList.size()) {
            throw new NoSuchElementException();
        }
        String temp = tipsList.get(tipsNumber);
        tipsNumber++;

        System.out.println(temp);
        return temp;
    }

    public boolean checkGuess(String guess) {
        return guess.equalsIgnoreCase(word);
    }
}
4
  • 1
    this line "temporaryTipsPanel.removeAll();" might have been an issue in some form…I can't make full sense of all the code cuz it's just a lot, but this is what seems to make the most sense since you are narrowing it to the showTips method Aug 7, 2013 at 18:26
  • And yet again i keep on repeating my self... Please paste only relevant code not the whole program. No one is gonna scan through all the lines for possible error. Aug 7, 2013 at 20:03
  • User2277872: Thanks for the answer. I'll try to take a look at that. Aug 8, 2013 at 7:38
  • 1
    Maciej Cygan: Sorry about that. I'm new here. But I'll keep that in mind. Thanks you. Aug 8, 2013 at 7:39

1 Answer 1

1

I figured it out!

I don't know why it can't be done but if I deleted this line: JPanel temporaryTipsPanel = new JPanel(new GridLayout(3, 1));

and placed it in another method it worked. If someone could explain why that would be great but at least I/we know what was the problem.

Thank you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.