Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:事件后组件消失_Java_Swing_User Interface_Jpanel - Fatal编程技术网

Java:事件后组件消失

Java:事件后组件消失,java,swing,user-interface,jpanel,Java,Swing,User Interface,Jpanel,我试图做一些有趣的东西,当我按下gui中的“ok”按钮后,我的组件就消失了 我正在尝试制作一个“猜一个单词”程序,其中一个会得到提示,然后你可以输入一个猜测,如果匹配,它会给你一条消息,如果不匹配,另一个提示。问题是,如果你输入的不是正确的单词,它会给你一个信息,告诉你它不是正确的单词,然后它会给你另一个提示。但另一个提示不会出现。它们消失了 我有两门课,“StartUp”和“QuizLogic”。 问题出现在“showTips”方法中的某个地方(我认为) 如果有人自己尝试,可在此处找到指向该文

我试图做一些有趣的东西,当我按下gui中的“ok”按钮后,我的组件就消失了

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

我有两门课,“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);
    }
}
公共类启动{
私有JFrame;
私有JButton showRulesYesButton;
私有JButton showRulesNoButton;
私人JButton checkButton;
私人JPanel mainBackgroundManager;
私人JTextField猜测中心;
私人QuizLogic QuizLogic;
私人ArrayList提示;
私人杰拉贝尔·菲斯特·提普拉贝尔;
二等兵杰拉贝尔;
私人JLabel thirdTipLabel;
//建造师
公共启动(){
//显示“启动”消息
showMessageDialog(null,“猜一个单词!”);
showRules();
}
//方法
公共规则(){
//基本框架法
框架=新的JFrame(“规则”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(新尺寸(500500));
frame.setLocationRelativeTo(空);
//创建JLabel并添加到JPanel
JLabel firstRule=newjlabel(“1.你一次只能得到一个提示,说明这个词可能是什么。”);
JLabel secoundRule=newjlabel(“2.你将得到三个提示和无限的猜测。”);
JLabel thirdRule=新的JLabel(“3.每个单词都是名词,并且以其基本形式”);
JLabel understand=新JLabel(“准备好了吗?”);
//创建JPanel并将JLabel添加到JPanel
JPanel temporaryRulesPanel=新的JPanel(新的FlowLayout(FlowLayout.CENTER,100,60));
临时规则spanel.add(第一条规则);
临时规则spanel.add(secoundRule);
临时规则增补(第三条);
临时规则spanel.添加(理解);
//初始化按钮
showRulesYesButton=新JButton(“是”);
showRulesNoButton=新的JButton(“否”);
showRulesYesButton.addActionListener(新的StartUpEventHandler());
showRulesNoButton.addActionListener(新的StartUpEventHandler());
//创建JPanel并将按钮添加到JPanel
JPanel temporaryButtonPanel=新的JPanel(新的FlowLayout(FlowLayout.CENTER,100,35));
临时按钮Panel.add(显示规则按钮);
临时按钮Panel.add(showRulesNoButton);
//初始化并将JPanel添加到JPanel
mainBackgroundManager=newjpanel(newborderlayout());
mainBackgroundManager.add(临时规则面板,BorderLayout.CENTER);
mainBackgroundManager.add(临时按钮面板,BorderLayout.SOUTH);
//将JPanel添加到JFrame
frame.add(mainBackgroundManager);
frame.setVisible(true);
}
公共无效clearBackground(){
mainBackgroundManager.removeAll();
quizLogic=新的quizLogic();
showGuessFrame();
showTips();
}
公共void showGuessFrame(){
JPanel guessPanel=新的JPanel(新的FlowLayout(FlowLayout.CENTER,50,10));
firstTipLabel=新的JLabel(“a”);
secondTipLabel=新的JLabel(“b”);
thirdTipLabel=新的JLabel(“c”);
tips=新的ArrayList();
guessEntery=新的JTextField(“在此处输入guess”,20);
checkButton=新的JButton(“Ok”);
checkButton.addActionListener(新的StartUpEventHandler());
添加(GuessEntry);
猜面板。添加(选中按钮);
mainBackgroundManager.add(猜测面板,BorderLayout.SOUTH);
frame.add(mainBackgroundManager);
}
公共空间展示技巧(){
JPanel temporaryTipsPanel=新JPanel(新网格布局(3,1));
临时Tipspanel.removeAll();
add(quizLogic.getTip());
System.out.println(tips.size());
如果(tips.size()==1){
firstTipLabel.setText(tips.get(0));
}
如果(tips.size()==2){
secondTipLabel.setText(tips.get(1));
}
如果(tips.size()==3){
thirdTipLabel.setText(tips.get(2));
}
临时TIPSpanel.add(第一个TIPLABEL);
临时TipPanel.add(第二个TipPanel);
临时TIPSpanel.add(第三个标签);
mainBackgroundManager.add(临时tipspanel,BorderLayout.CENTER);
frame.add(mainBackgroundManager);
frame.revalidate();
frame.repaint();
}
公共无效getGuess(){
字符串temp=guessEntry.getText();
boolean correctAnswer=quizLogic.checkGuess(guessEntry.getText());
if(correctAnswer==true){
showMessageDialog(null,“干得好!单词是”+temp);
}
否则{
showMessageDialog(null,temp+“不是我们要找的词”);
showTips();
}
}
私有类StartUpEventHandler实现ActionListener{
已执行的公共无效操作(操作事件){
if(event.getSource()==showRulesYesButton){
clearBackground();
}
else if(event.getSource()==showRulesNoButton){
showMessageDialog(null,“再次阅读规则”);
}
else if(event.getSource()==checkButton){
getGuess();
}
}
}
}
公共类QuizLogic{
私人ArrayList quizWord;
私人数组列表;
私人国际酒牌号码;
私有字符串字;
公共QuizLogic(){
酒号