首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TroubleShooting JOptionPane错误

TroubleShooting JOptionPane错误
EN

Stack Overflow用户
提问于 2016-11-28 10:10:08
回答 1查看 64关注 0票数 0

我有一个问题,我有一个程序,我想测试用户记忆随机颜色列表的能力。根据用户输入的是对还是错,它将询问下一个颜色。

所以我把所有的工作都做到了用户输入第一种颜色的地方。在第一种颜色上有用户输入之前。程序已经假设用户输入是错误的,即使它没有要求任何输入。

根据我之前的知识,我可以刷新缓冲区,你能用JOptionPane做到这一点吗?

或者这是我没有看到的另一个问题?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-11-28 10:16:07

这与刷新缓冲区无关。

您将在这里获得用户输入:guess = txtInput.getText();,它位于intializeGame方法中。这意味着在用户有机会在字段中输入任何内容之前,您将在创建时从txtInput JTextField获取文本。我认为您习惯于编写线性控制台程序,在这种程序中,您可以立即获得用户的输入,但这不是事件驱动GUI的工作方式。相反,您必须获取并响应用户在event上的输入,这里可能是某个按钮的ActionListener。也许您的代码需要一个“提交”JButton或类似的东西,并在其ActionListener中从JTextField中提取输入并对其进行响应。这样做,你的代码就会有更好的工作机会。

其他问题:

  • 您似乎从未将txtInput JTextField添加到图形用户界面中。
  • homeScreen JLabel

也是如此

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40836198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档