简单 FlowLayout 帮助
我对这个程序有几个问题,我想做的第一件事就是让它可以比较并查看文本字段是否等于 colorValues[x] 位置。第二个问题是 if 语句说 if inText == to colorValues.length - 1 打开一个表示祝贺的框,但这也不起作用。第三个问题,即使它确实收到抱歉消息和/或祝贺消息,您如何做到不显示文本字段?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlbertCardonaProg7 extends JFrame
{
private static final int WIDTH = 350;
private static final int HEIGHT = 250;
private static final String[] colorValues = {"red","white",
"yellow","green","blue"};// I dentifies the colors
private JTextField nameBox;
private JLabel greeting;
private String[] message = {"Input color number 1",
"Input color number 2: ","Input color number 3: "
,"Input color number 4:","Input color number 5:"};
private JLabel namePrompt = new JLabel(this.message[0]);
public AlbertCardonaProg7()
{
setTitle("MEMORY GAME");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}// end constructor
//******************************************
private void createContents()
{
nameBox = new JTextField(15);
greeting = new JLabel();
add(namePrompt);
add(nameBox);
add(greeting);
nameBox.addActionListener(new Listener());
}//end createContents
//************************************************
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int inText;
for(inText =0; inText < 5; inText++)
{
if(nameBox.getText().equals(colorValues[inText] ))
{
namePrompt.setText( message[inText]); // its not working trying
//to see if it is equal to the proper spot
//in the colorValues[array]
add(nameBox);
nameBox.setText("");
nameBox.requestFocus();
inText++;
}
if(!nameBox.getText().equals(colorValues[inText]))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Sorry, drink more Ginseng ");
add(namePrompt);
break;
}
if( inText == (colorValues.length -1))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Congradulations,
Your mind is Awesome!!!");
add(namePrompt);
break;
}
}// loop
}//end action performed
}// end class Listener
//**************************************
public static void main(String[] args)
{
String colors = "";
for(int i = 0; i < colorValues.length; i++)
colors += colorValues[i] + " ";
JOptionPane.showMessageDialog(null,"How good is your memory.\n
See if you can memorize this sequence.\n\n" + colors,
"Message", JOptionPane.INFORMATION_MESSAGE );
AlbertCardonaProg7 outBox = new AlbertCardonaProg7();
}// end main class
}//end Class AlberCardonaProg7
I have a few questions for this program, one the first thing I am trying to do is make it so it could compare and see if the textfield is equal to the colorValues[x] position. The second issue is the if statement says if inText == to colorValues.length - 1 to open a box that says congradulations that does not work either. 3rd issue even if it did get the Sorry message and or congradulations message how do you make it so that the textfield is not shown?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlbertCardonaProg7 extends JFrame
{
private static final int WIDTH = 350;
private static final int HEIGHT = 250;
private static final String[] colorValues = {"red","white",
"yellow","green","blue"};// I dentifies the colors
private JTextField nameBox;
private JLabel greeting;
private String[] message = {"Input color number 1",
"Input color number 2: ","Input color number 3: "
,"Input color number 4:","Input color number 5:"};
private JLabel namePrompt = new JLabel(this.message[0]);
public AlbertCardonaProg7()
{
setTitle("MEMORY GAME");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}// end constructor
//******************************************
private void createContents()
{
nameBox = new JTextField(15);
greeting = new JLabel();
add(namePrompt);
add(nameBox);
add(greeting);
nameBox.addActionListener(new Listener());
}//end createContents
//************************************************
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int inText;
for(inText =0; inText < 5; inText++)
{
if(nameBox.getText().equals(colorValues[inText] ))
{
namePrompt.setText( message[inText]); // its not working trying
//to see if it is equal to the proper spot
//in the colorValues[array]
add(nameBox);
nameBox.setText("");
nameBox.requestFocus();
inText++;
}
if(!nameBox.getText().equals(colorValues[inText]))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Sorry, drink more Ginseng ");
add(namePrompt);
break;
}
if( inText == (colorValues.length -1))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Congradulations,
Your mind is Awesome!!!");
add(namePrompt);
break;
}
}// loop
}//end action performed
}// end class Listener
//**************************************
public static void main(String[] args)
{
String colors = "";
for(int i = 0; i < colorValues.length; i++)
colors += colorValues[i] + " ";
JOptionPane.showMessageDialog(null,"How good is your memory.\n
See if you can memorize this sequence.\n\n" + colors,
"Message", JOptionPane.INFORMATION_MESSAGE );
AlbertCardonaProg7 outBox = new AlbertCardonaProg7();
}// end main class
}//end Class AlberCardonaProg7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我建议您学习正确地格式化代码,因为这样可以更轻松地了解正在发生的情况。
其次,您发布的代码在第 64 行和第 80 行存在语法错误,导致编译失败。问题是 Java 不允许您在源代码中使用多行字符串文字,因此您必须将两个字符串连接在一起。例如:
应该是:
现在,不幸的是,您的问题并没有特别清楚地说明程序的预期行为应该是什么。我对此的解释是,您希望向用户提供文本框,要求他们输入第一种颜色,然后显示一个对话框,根据他们是否得到正确的答案来表示祝贺或抱歉。如果他们得到的答案正确,那么您想要显示第二种颜色的输入框,检查答案等。
我的第一个建议是在实例化 JFrame 时创建所有控件,但只需隐藏其他控件,直到用户输入了正确的值。接下来,我建议您在编写代码之前计划好操作侦听器将要执行的操作。
在这种情况下,程序需要存储用户当前正在处理的输入字段的数组索引。然后,侦听器需要检查此变量,并验证 inputFields 数组中的相应字段。侦听器需要向用户显示一个对话框,说明他们的答案是否正确,如果用户正确,则启用下一个输入字段。
将所有内容放在一起,您会得到:
编辑:根据您下面的评论,我修改了程序以满足预期的行为。主要的变化是构造函数不再隐藏其他控件,并且侦听器现在必须循环每个输入字段以检查它们是否正确:
First of all I'd suggest that you learn to format your code properly, as it makes it much easier to see whats going on.
Secondly, the code you've posted has syntax errors on lines 64 and 80 which cause it to fail compilation. The issue is that Java doesn't allow you to have multi line string literals within the source code, so you have to concatenate the two string together. For example:
should be:
Now, unfortunately your question doesn't make it particularly clear as to what the intended behavior of the program should be. My interpretation of it, is that you want to provide the user with the text box asking them to enter the first color, and then display a dialog saying either congratulations or sorry depending on whether they got the answer correct. If they get the answer correct, then you want to display the input box for the second color, check the answer, etc.
My first suggestion is to create all of the controls when you instantiate the JFrame, but simply hide the other ones until the user has entered the correct value. Next, I'd suggest that you plan what the action listener is going to do before you go in and write the code.
In this case, the program needs to store the array index of the input field that the user is currently working on. The listener then needs to check this variable, and validate the corresponding field in the inputFields array. The listener needs to display a dialog to the user saying whether they got the answer correct, and if the user did, enable the next input field.
Putting it all together you get this:
Edit: based on your comment below, I've modified the program to meet the expected behavior. The main changes are the constructor no longer hides the other controls, and the listener now has to loop over each input field to check they are all correct: