我有一个问题,我有一个程序,我想测试用户记住随机颜色列表的能力。如果用户输入是正确的或错误的,它会要求下一个颜色。疑难解答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
嗯,我一直试图做一段时间,所以我已经尝试了很多不同的东西,我有一些不同的东西。所以我添加了我的其他程序,我尝试了你所说的(因为我已经尝试了类似的东西)。但是,我如何清除:“你准备好了文字”和按钮?然后添加txtinput的东西 – noreturn
@noreturn:如果你想交换“显示”,然后使用CardLayout。该教程可以在这里找到:[CardLayout教程](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html),但这是一个完全不同的问题。 –
另外我试过使用:homeScreen.setVisible(false); btnOK.setVisible(false); 我不想交换显示器,应该都在同一个窗口中。只需旋转即可。 – noreturn