2016-11-28 109 views
0

我有一個問題,我有一個程序,我想測試用戶記住隨機顏色列表的能力。如果用戶輸入是正確的或錯誤的,它會要求下一個顏色。疑難解答JOptionPane錯誤

所以我得到了這一切工作到用戶輸入第一種顏色。在用戶輸入第一種顏色之前。該程序已經假定用戶輸入是錯誤的,即使它沒有要求任何輸入。

我知道從以前的知識我可以喜歡刷新緩衝區,你可以用JOptionPane做到這一點?

或者這是我沒有看到的另一個問題?

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

public class Testing 
{ 
    //Intialization of the whole program for everything to pass information 
    public JFrame main; 
    public JLabel lbInsturctions; 
    public JLabel welcomeMessage; 
    public JLabel homeScreen; 
    public JLabel homeScreenColors; 
    public JTextField txtInput; 
    public int num = 1; 
    public String colorList = ""; 
    public String[] color = {"red","green","blue","brown","yellow", "gold", "orange", "silver"}; 
    public String[] solution = new String[5]; 

    //sets up the window and random colors 
    public Testing() 
    { 
     Random r = new Random(); 

     for (int i = 0; i<solution.length; i++) 
     { 
     solution[i] = color[r.nextInt(7)]; 
     colorList = Arrays.toString(solution); 
     } 

     JOptionPane.showMessageDialog(null, "Lets test your memory. Memorize these colors: " + colorList); 

     main = new JFrame(); 
     main.setSize (500,300); 
     main.setTitle ("Ultimate Colors"); 
     main.setDefaultCloseOperation(main.EXIT_ON_CLOSE); 
     main.setLayout(new FlowLayout()); 

     intializeGame(); 
     main.setVisible(true); 
    } 


    public void intializeGame() 
    { 
     //All Intiazations 
     lbInsturctions = new JLabel(); 
     homeScreen = new JLabel(); 
     txtInput= new JTextField(null, 15); 


     //Need to delete or make new window if user pushes ok then 
     lbInsturctions.setText("Enter color number " + num + ":"); 
     main.add(lbInsturctions); 
     main.add(txtInput); 

     txtInput.addActionListener(new colorTester());  
    } 

    public class colorTester implements ActionListener 
    { 

     public void actionPerformed (ActionEvent e) 
     { 


     //Need to delete or make new window if user pushes ok then 
     lbInsturctions.setText("Enter color number " + num + ":"); 

     //grabs the users input to see if it is corect 
     String guess= ""; 
     guess = txtInput.getText(); 

     System.out.println(guess); 

     //Checks to see if the users input is the same as the initalizaton 
     if (color[num+1].equalsIgnoreCase(guess) || num > 6) 
     { 
      System.out.println("You got it!"); 
      ++num; 

      lbInsturctions.setText("Enter color number " + num + ":"); 
      txtInput.setText(""); 
     } 

     //if the User input is wrong 
     else 
     { 
      System.out.println("It's a good thing your not graded!"); 
      txtInput.setVisible(false); 
      lbInsturctions.setText("It's a good thing this is not graded!"); 
      } 

      if (num == 5) 
      { 
      lbInsturctions.setText("You memory is perfect good job!"); 
      txtInput.setVisible(false); 
      } 

     } 




    } 


}//end of program 

回答

1

這有什麼做沖洗緩衝區。

您在這裏獲得用戶輸入:guess = txtInput.getText();這是在intializeGame方法。這意味着在用戶有機會在字段中輸入任何內容之前,您將從txtInput JTextField創建文本中獲取文本。我認爲你習慣於編程線性控制檯程序,你可以立即得到用戶的輸入,但這不是事件驅動GUI的工作方式。相反,您必須在事件上得到用戶的輸入並對其做出反應,這裏可能是某個按鈕的ActionListener。也許你的代碼需要一個「提交」JButton或類似的東西,並且在它的ActionListener中,從JTextField中提取輸入並作出響應。做到這一點,你的代碼有更好的工作機會。

其他問題:

  • 你永遠不要似乎已經加上你txtInput的JTextField進入GUI。
  • 同爲JLabel的主屏幕

編輯您的代碼張貼在你的問題的底部有同樣的問題。

+0

嗯,我一直試圖做一段時間,所以我已經嘗試了很多不同的東西,我有一些不同的東西。所以我添加了我的其他程序,我嘗試了你所說的(因爲我已經嘗試了類似的東西)。但是,我如何清除:「你準備好了文字」和按鈕?然後添加txtinput的東西 – noreturn

+0

@noreturn:如果你想交換「顯示」,然後使用CardLayout。該教程可以在這裏找到:[CardLayout教程](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html),但這是一個完全不同的問題。 –

+0

另外我試過使用:homeScreen.setVisible(false); btnOK.setVisible(false); 我不想交換顯示器,應該都在同一個窗口中。只需旋轉即可。 – noreturn