1

我正在尝试制作一些有趣的东西,并且在我按下 gui 中的“确定”按钮后,我的组件不断消失。

我正在尝试制作一个“猜一个词”程序,其中一个人会得到一个小费,然后你可以输入一个猜测,如果它匹配它会给你一个消息,如果不是,另一个提示。问题是,如果你输入的不是这个词,它会给你一个信息,它不是正确的词,然后它会给你另一个提示。但另一个提示不会出现。他们消失了。

我有两个课程,“StartUp”“QuizLogic”。问题出现在“showTips”方法的某个地方(我认为)。

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

谢谢你。

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 回答 1

1

我想到了!

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

并将其置于另一种有效的方法中。如果有人可以解释为什么那会很棒,但至少我/我们知道问题出在哪里。

谢谢你。:)

于 2013-08-08T13:28:19.283 回答