2013-08-07 5 views
1

내 GUI에서 "ok"버튼을 누르면 재미있는 항목을 만들고 내 구성 요소가 계속 사라집니다.Java : 이벤트 발생 후 사라지는 구성 요소

"단어 추측" 프로그램을 만들려고합니다. 팁 하나를 얻은 다음 추측을 입력하고 일치하면 메시지와 그렇지 않은 경우 다른 팁을 제공합니다. 문제는 단어가 아닌 것을 입력하면 올바른 단어가 아니며 다른 팁을 알려주라는 메시지를 줄 것입니다. 그러나 다른 팁은 나타나지 않을 것입니다. 그들은 사라진다.

두 클래스가 있습니다. "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

이 줄은 "temporaryTipsPanel.removeAll();"입니다. 어떤 형식으로 문제가되었을 수도 있습니다 ... 모든 코드를 이해할 수는 없지만 showTips 메서드로 좁히기 때문에 가장 의미가있는 것 같습니다. – user2277872

+0

그리고 다시 저는 내 자신을 반복해서 계속 ... 전체 프로그램이 아닌 관련 코드 만 붙이십시오. 아무도 모든 라인을 통해 오류를 찾아 낼 수 없습니다. –

+0

User2277872 : 답변 해 주셔서 감사합니다. 나는 그것을 살펴 보려고 노력할 것이다. –

답변

1

알아 냈습니다.

이 할 수없는 이유를 모르겠어요하지만이 줄을 삭제 한 경우 : JPanel temporaryTipsPanel = new JPanel(new GridLayout(3, 1));

을하고 다른 방법에 배치 일했다. 누군가가 왜 그렇게 대단한지를 설명 할 수는 있지만 적어도 나는/그 문제가 무엇인지 압니다.

감사합니다. :)