gpt4 book ai didi

java - JOptionPane 错误故障排除

转载 作者:行者123 更新时间:2023-12-01 09:12:22 24 4
gpt4 key购买 nike

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

所以我把一切都完成了,直到用户输入第一种颜色。在用户输入第一种颜色之前。该程序已经假设用户输入是错误的,即使它没有要求任何输入。

根据之前的知识,我知道我可能想刷新缓冲区,您可以使用 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 的工作方式。相反,您必须获取用户的输入并对其使用react事件,这里可能是某个按钮的 ActionListener。也许您的代码需要一个“提交”JButton 或类似的东西,并在其 ActionListener 中从 JTextField 中提取输入并对其进行响应。这样做,您的代码就有更好的机会工作。

其他问题:

  • 您似乎从未将 txtInput JTextField 添加到 GUI 中。
  • 主屏幕 JLabel 也是如此
<小时/>

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

关于java - JOptionPane 错误故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com