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

标签 java swing user-interface jpanel

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

我正在尝试制作一个“猜一个单词”程序,其中一个人会得到一个提示,然后您可以输入一个猜测,如果匹配,它会给您一条消息,如果不匹配,则会给您另一个提示。问题是,如果您输入的内容不是该单词,它会向您显示一条消息,表明该单词不正确,然后会给您另一个提示。但另一个提示不会显示。他们消失了。

我有两个类(class),“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);
    }
}

最佳答案

我明白了!

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

并将其放入另一种有效的方法中。如果有人能解释为什么那就太好了,但至少我/我们知道问题是什么。

谢谢。 :)

关于Java:组件在事件发生后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110879/

相关文章:

java - 关于ActionListener.image编辑的问题

java - 使用 Swingworker 不断更新 GUI

python - 如何绑定(bind)到 Tkinter 中的所有数字键?

javascript - ExtJS 垂直滚动条不适合长 json 数据

java - 将 JAXB 注释应用于现有数据模型

java - 如何制作一个在系统的某个时间启动的方法?

Java Swing 。绘画组件

Java JList 仅在最小化或最大化后刷新

java - Oracle 是否仍支持 Java DMK(或 OpenDMK)?

java - 优化 Long.bitCount