【问题标题】:TroubleShooting JOptionPane errorJOptionPane 错误疑难解答
【发布时间】:2026-02-04 19:55:02
【问题描述】:

我有一个问题,我有一个程序想测试用户记住随机颜色列表的能力。根据用户输入的正确或错误,它会询问下一种颜色。

所以我得到了所有的工作,直到用户输入第一个颜色。在用户输入第一种颜色之前。程序已经假设用户输入错误,即使它没有要求任何输入。

从以前的知识中我知道我可以刷新缓冲区,你能用 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

【问题讨论】:

    标签: java swing event-handling joptionpane flush


    【解决方案1】:

    这与刷新缓冲区无关

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

    其他问题:

    • 您似乎从未将 txtInput JTextField 添加到 GUI 中。
    • homeScreen JLabel 也是如此

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

    【讨论】:

    • 好吧,我一直在尝试这样做一段时间,所以我尝试了很多不同的东西,我有一些不同的东西。所以我添加了我的另一个程序,我尝试了你所说的(因为我已经尝试过类似的东西)。但是,我如何清除:“你准备好文本”和按钮?然后添加txtinput的东西
    • @noreturn:如果要交换“显示”,请使用 CardLayout。教程可以在这里找到:CardLayout tutorial,但这是一个完全不同的问题。
    • 我也试过使用:homeScreen.setVisible(false); btnOK.setVisible(false);我不想交换显示器,应该都在同一个窗口中。只需旋转即可。
    • 然后调用revalidate()repack() 在进行任何更改后保存这些组件(主的 contentPane)的 容器,以便它们显示在 GUI 中。
    • @noreturn:但同样,这些都是其他问题。您的主要问题源于过早获取用户的输入,并且解决方案再次是不这样做,仅在用户指示应该通过 JButton 或 JTextField ActionListener 提交时获取用户的输入。