Java:组件在事件发生后消失

时间:2013-08-07 18:22:33

标签: java swing user-interface jpanel

我试图制作一些有趣的东西,然后按下" ok"我的组件一直在消失。在我的gui按钮。

我试图制作一个"猜一个单词" 程序,其中一个人会得到一个提示,然后你可以输入一个猜测,如果它匹配它将给你一个消息,如果没有,另一个提示。问题是,如果你输入的内容不是单词,它会给你一个消息,说明它不是正确的单词,然后会给你另一个提示。但另一个提示不会出现。他们消失了。

我有两个课程," StartUp" " QuizLogic" 。 问题出现在" showTips"方法(我认为)。

如果有人自己尝试,可以在此处找到该文件的链接:Link to file

谢谢。

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);
    }
}

1 个答案:

答案 0 :(得分:1)

我明白了!

我不知道为什么不能这样做但是如果我删除了这一行:JPanel temporaryTipsPanel = new JPanel(new GridLayout(3, 1));

并将其置于另一种工作方式中。如果有人可以解释为什么那会很好,但至少我/我们知道问题是什么。

谢谢。 :)