故障排除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

这与冲洗缓冲区有关

您在此处获取用户输入:guess = txtInput.getText();intializeGame方法中。这意味着您正在从txtinput jtextfield创建中获取文本,在用户有机会将任何内容输入字段之前。我认为您习惯于对线性控制台程序进行编程,您可以立即获取用户的输入,但这不是事件驱动的GUI的工作方式。取而代之的是,您必须在Event 上对用户的输入做出反应,这也许是某些按钮的动作位置。也许您的代码需要"提交" jbutton或类似的内容,并且在其ActionListener中,从JTEXTFIELD提取输入并响应它。这样做,您的代码有更好的工作机会。

其他问题:

  • 您似乎永远不会将您的txtinput jtextfield添加到GUI中。
  • 与Homescreen Jlabel相同

编辑您的问题底部发布的新代码也有相同的问题。

相关内容

  • 没有找到相关文章

最新更新