2014-12-03 80 views
0

我創建了一個對話框,並讓用戶從內存中輸入5種顏色。這一切都完全有效,這只是一個輕微的美學問題。在正確輸入所有5種顏色或獲取一種不正確的顏色後,假設擦除對話框中的內容並打印「對不起!顏色不正確」或「恭喜」的消息。它會打印該消息,但JTextField仍然可以在消息後面看到(留下部分/裁剪)。我嘗試使用hide()和remove()方法,但他們似乎並沒有工作(或我使用它們不正確),我試圖重新制作對話框,但我不能' t似乎仍然解決了這個問題。我在做什麼錯了/我怎樣才能讓JTextField在完成後消失?預先感謝您的任何幫助!JTextField將不會完全消失

這裏,如果用戶輸入錯誤的顏色或讓他們在那裏的部分正確的(txtName的是JTextField的):

 if(count == 6)//User either finished or entered a color incorrectly 
     {     
      //Entered color incorrectly 
      if(incorrect == true) 
      {      
       txtName.setEnabled(false); //Doesn't work 
       homeScreen.remove(txtName); //Doesn't work 
       labelName.setText("Incorrect! Sorry - Wrong color."); 
       //txtName.removeActionListener(new MyButtonListener()); 
      } 
      else//Correctly finished the game. 
      { 
       labelName.setText("Congratulations - your memory skills are perfect!"); 
       //txtName.removeActionListener(new MyButtonListener()); 
       homeScreen.remove(txtName);//Doesn't work 
      } 
     } 

這裏是我的整個程序(我不能讓它在後正確地格式化):

package memorygame; 

import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.FlowLayout; 

public class MemoryGame 
{ 
    private JFrame homeScreen; 
    private JLabel labelName; 
    private JTextField txtName; 
    private JLabel correct; 
    Vector<String> name = new Vector(); 
    private int count = 1; 

    private MyButtonListener listen1 = new MyButtonListener(); 

    //Constructor - Method to be called when MemoryGame object called 
    public void MemoryGame() 
    { 
     homeScreen = new JFrame();  
     homeScreen.setSize(400,200); 
     homeScreen.setTitle("Memory Game"); 
     homeScreen.setDefaultCloseOperation(homeScreen.EXIT_ON_CLOSE); 
     homeScreen.setLayout(new FlowLayout()); 
     labelName = new JLabel(); 
     txtName = new JTextField(10); 
     createContents(); 
     homeScreen.setVisible(true); 
    }//End Constructor 

    //Create components and add them to the window/dialog box 
    private void createContents() 
    {  
     labelName.setText("Enter the color " + count + ":"); 
     System.out.println("The current count is: " + count); 
     homeScreen.add(labelName); 
     homeScreen.add(txtName); 
     txtName.addActionListener(new MyButtonListener());//Allows you to press enter to invoke action 
    } 
    //Upon user hitting enter 
    private class MyButtonListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e)//When event occurs 
     { 
      Scanner in = new Scanner (System.in);//For program input 
      String answer = ""; 
      //Make memColor an array for randomized colors 
      /* 

      Random r = new Random(); 
      String[] memColors = new String[5];    
      String[] colors = {"red", "green", "blue", "yellow", "brown", "purple"}; 

      for(int i =0; i < memColors.length; i++) 
      { 
       memColors[i] = colors[r.nextInt(6)]; 
      } 
      */ 
      String memColor1 = "red"; 
      String memColor2 = "black"; 
      String memColor3 = "yellow"; 
      String memColor4 = "green"; 
      String memColor5 = "blue"; 
      boolean incorrect = false; 

      //If answered incorrectly set count to 5(it'll be 6) 
      //And have a boolean for if count== 6 for congrats and failure 
      if(e.getSource() == txtName) 
      { 
       answer = txtName.getText(); 
       System.out.println(answer); 
      } 
      else 
      {}  
      //Check if user entered Correct color, 1= Red, 2= Black, etc. 
      if(count == 1) 
      { 
       if(answer.equalsIgnoreCase(memColor1)) 
       { 
        txtName.setText("");     
       } 
       else 
       {//Needs to be a custom message box 
        count = 5; 
        incorrect = true; 
       }     
      } 
      else if(count == 2) 
      { 
       if(answer.equalsIgnoreCase(memColor2)) 
       { 
        txtName.setText("");  
       } 
       else 
       {     
        count = 5; 
        incorrect = true; 
       }     
      }    
      else if(count == 3) 
      { 
       if(answer.equalsIgnoreCase(memColor3)) 
       { 
        txtName.setText("");     
       } 
       else 
       { 
        count = 5; 
        incorrect = true; 
       } 
      } 
      else if(count == 4) 
      { 
       if(answer.equalsIgnoreCase(memColor4)) 
       { 
        txtName.setText("");     
       } 
       else 
       { 
        count = 5; 
        incorrect = true; 
       } 
      } 
      else if(count == 5) 
      { 
       if(answer.equalsIgnoreCase(memColor5)) 
       { 
        txtName.setText(""); 

       } 
       else 
       { 
        count = 5; 
        incorrect = true; 
       } 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, "Something went wrong!"); 
      }    

      count += 1; 
      //User has completed the game or entered a color incorrectly 
      if(count == 6) 
      { 

       if(incorrect == true) //Incorrect color 
       {      
        txtName.setEnabled(false); 
        homeScreen.remove(txtName); 
        labelName.setText("Incorrect! Sorry - Wrong color."); 
        //txtName.removeActionListener(new MyButtonListener()); 
       } 
       else //Completed the game correctly 
       { 
        labelName.setText("Congratulations - your memory skills are perfect!"); 
        //txtName.removeActionListener(new MyButtonListener()); 
        homeScreen.remove(txtName); 
       } 
      } 
      else 
      { 
       labelName.setText("Enter the color " + count + ":"); 
      }    
     }//End Listener   
    }//End Button class 

     public static void main(String[] args) {  

     //Show message box 
     //Randomize colors 
     JOptionPane.showMessageDialog(null, "How good is your memory?\nTry to memorize this color sequence:\n\n red black yellow green blue"); 

     MemoryGame mem = new MemoryGame();   
     mem.MemoryGame();   
    }//End Main 
}// End Class 
+1

嘗試使用'txtName.setVisible(false);'而不是'homeScreen.remove(txtName);' – MadProgrammer 2014-12-03 04:22:40

回答

1

使用txtName.setVisible(false);代替homeScreen.remove(txtName);

基本上,如果你想打電話remove,您將需要revalidaterepaint容器......

你還需要確保你的UI是事件調度線程的上下文中創建,看Initial Threads更多細節

+0

謝謝,工作!我不明白事件派發線程的內容是什麼,但我會從該鏈接更多地考慮它。 – Riotson 2014-12-03 04:30:45

+0

這是主要的UI線程,這很重要... – MadProgrammer 2014-12-03 04:31:21

1

更改代碼

homeScreen.remove(txtName); 

homeScreen.remove(txtName); 
homeScreen.revalidate(); 
homeScreen.repaint(); 

之所以remove()並不意味着revalidate() + repaint()remove()不是原子的。來電者可能想要執行多個更新,即幾個add()remove()調用的序列。 revalidate()基本上「完成」你的「UI更新事務」,repaint()「將它推到屏幕上」。

作爲一個方面說明,如果您對變量名稱執行小的改進,您的代碼將更易於理解和維護。什麼是homeScreen?爲什麼叫labelName - 什麼名字?什麼是txtName - 什麼文字的名字?什麼,冰淇淋的count

,我建議如下改進:

  • incorrect - >isIncorrect(也改變if (incorrect == true)if (isIncorrect)
  • homeScreen - >mainFrame或只是frame(因爲你只需要一幀)
  • labelName - >infoLabel或只是label(因爲您只有一個標籤 - 並刪除JLabel correct,它未使用)
  • txtName - >answerTextField
  • count - >answerCount

刪除變量listen1,它不使用。

另外,如果您查看if (count == 1)和以下四個if子句的代碼,它們除了數字之外都是相同的。陣列的完美情況。您可以將變量memColor*轉換爲數組String[] memColor。或者也許這就是Vector的用途。您可能想要使用ArrayList,但在這種情況下,目前沒有人使用Vector

+0

非常感謝您閱讀我的整個代碼並指出我可以改進的地方並解釋重新驗證和重繪方法!我目前正在使用這些更新修復我的代碼。我曾計劃將memColor作爲一個數組,但我沒有想到將if語句作爲數組來做。謝謝! – Riotson 2014-12-03 04:59:33