Java JFormattedTextField和Integer对象之间的比较
我有点奇怪的问题 我正在写一个简单的数独程序。现在,除此方法外,其他方法都能正常工作:Java JFormattedTextField和Integer对象之间的比较,java,swing,integer,jformattedtextfield,Java,Swing,Integer,Jformattedtextfield,我有点奇怪的问题 我正在写一个简单的数独程序。现在,除此方法外,其他方法都能正常工作: public static void checkAnswerKey() { int rowCounter = 0; int columnCounter = 0; System.out.println("The answers that are correct are (F is incorrect): "); for (rowCoun
public static void checkAnswerKey()
{
int rowCounter = 0;
int columnCounter = 0;
System.out.println("The answers that are correct are (F is incorrect): ");
for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
{
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
if (gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString())
{
gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.correctAnswer);
System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + " ");
}
else
{
gui.userInputFormattedArray[rowCounter][columnCounter].setBackground(gui.incorrectAnswer);
System.out.print(gui.userInputFormattedArray[rowCounter][columnCounter].getText() + io.puzzleAnswerArray[rowCounter][columnCounter].toString() + " ");
//System.out.print("F" + " ");
}
}
System.out.println();
}
public static JFormattedTextField[][] userInputFormattedArray = new JFormattedTextField[9][9];
//Create the container.
public Container pane = getContentPane();
//The font the game uses.
public Font gameFont = new Font("Arial", Font.PLAIN, 30);
//Correct and incorrect answer colors.
static Color correctAnswer = new Color(100, 255, 100);
static Color incorrectAnswer = new Color(255, 100, 100);
这两种方法在构造函数中启动:
public void showTextFields()
{
int rowCounter = 0;
int columnCounter = 0;
for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
{
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
userInputFormattedArray[rowCounter][columnCounter] = new JFormattedTextField();
pane.add(userInputFormattedArray[rowCounter][columnCounter]);
userInputFormattedArray[rowCounter][columnCounter].setFont(gameFont);
userInputFormattedArray[rowCounter][columnCounter].setHorizontalAlignment(JTextField.CENTER);
}
}
}
public void setTextFields()
{
String heldString;
boolean notEditable = false;
int rowCounter = 0;
int columnCounter = 0;
for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
{
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
if (game.puzzleUserShownArray[rowCounter][columnCounter] != 0)
{
heldString = game.puzzleUserShownArray[rowCounter][columnCounter].toString();
userInputFormattedArray[rowCounter][columnCounter].setText(heldString);
userInputFormattedArray[rowCounter][columnCounter].setEditable(notEditable);
}
}
}
}
public void showTextFields()
{
int rowCounter=0;
int columnCounter=0;
对于(rowCounter=0;rowCounter,您正在使用=
进行字符串相等性检查
if(gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString()) {
使用.equals()
方法。这不仅适用于字符串,而且适用于任何类型的对象。对于对象==
检查对象的标识
尝试:
您正在使用==
进行字符串相等性检查
if(gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString()) {
使用.equals()
方法。这不仅适用于字符串,而且适用于任何类型的对象。对于对象==
检查对象的标识
尝试:
谢谢你,你的答案很完美。很高兴知道。谢谢你,你的答案很完美。很高兴知道。
if(gui.userInputFormattedArray[rowCounter][columnCounter].getText() == io.puzzleAnswerArray[rowCounter][columnCounter].toString()) {
if(gui.userInputFormattedArray[rowCounter][columnCounter].getText().equals(io.puzzleAnswerArray[rowCounter][columnCounter].toString())) {