2016-11-28 2 views
0

質問があります。ランダムな色のリストを覚えているユーザーをテストするプログラムがあります。ユーザーの入力が正しいか間違っているかに基づいて、次の色を要求します。TroubleShooting 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

これはフラッシングバッファを行うには何もを持っていません。

intializeGameメソッドにあるguess = txtInput.getText();のユーザー入力があります。つまり、ユーザがフィールドに何かを入力する前に、作成時にtxtInput JTextFieldのテキストがになっていることを意味します。私は、リニアコンソールプログラムをプログラミングすることに慣れていると思います。ここでは、ユーザーの入力を直ちに取得しますが、それはイベント駆動型GUIの動作ではありません。代わりに、イベントのユーザーの入力を取得して反応する必要があります。ここでは、おそらくボタンのActionListenerです。おそらく、あなたのコードは "submit" JButtonかそれに類するものを必要とし、そのActionListenerではJTextFieldから入力を抽出してそれに応答します。これとあなたのコードはより良い仕事の機会を持ちます。

その他の問題:

  • あなたがGUIにあなたのtxtInputのJTextFieldを追加しているように見えません。あなたの新しいコードは、あなたの質問の一番下に掲載したホームスクリーンのJLabel

編集のために同じ

  • は同じ問題を抱えています。

  • +0

    私はしばらくこのことをやろうとしていたので、私はいくつかの異なることがたくさんあった。だから私はあなたが言ったことを試した私の他のプログラムを追加しました。しかし、どのように私は: "あなたは準備ができているテキスト"とボタンをクリアするのですか?次に、txtinputのものを追加してください。 – noreturn

    +0

    @noreturn: "ディスプレイ"を交換する場合は、CardLayoutを使用してください。このチュートリアルは、[CardLayout tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)にありますが、これはまったく別の問題です。 –

    +0

    また、私は使ってみました:homeScreen.setVisible(false); btnOK.setVisible(false); 私はディスプレイを交換したくない、すべてが同じウィンドウにあるべきである。単に回転する。 – noreturn