gpt4 book ai didi

java - JTextField无法在if语句java中使用输入

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

我目前正在处理一项任务,要求我创建自定义缺失字段异常。我必须从 GUI 获取数据并确保所有字段都收到输入。我遇到的问题是,每当我使用 .getText() 获取输入并将其放入字符串中时,我无法在任何类型的 if 语句中使用它。我的意思是,如果我完全使用我所知道的 JTextField 内容编写 if 语句,它将不起作用。我对java有点陌生,所以我可能会错过一些很容易的东西。

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class PersonFrame extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
static LinkedList<Person> listOfObjects = new LinkedList<Person>();
JTextField fName;
JTextField lName;
JTextField Height;


public PersonFrame()
{
setTitle("Person");
setSize(200, 130);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,2));
add(new JLabel("First Name"));
fName = new JTextField();
add(fName);
add(new JLabel("Last Name"));
lName = new JTextField();
add(lName);
add(new JLabel("Height"));
Height = new JTextField();
add(Height);
JButton jbtSubmit = new JButton("Submit");
JButton jbtCancel = new JButton("Cancel");
add(jbtSubmit);
add(jbtCancel);
SubmitListenerClass listener1 = new SubmitListenerClass();
CancelListenerClass listener2 = new CancelListenerClass();
jbtSubmit.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
setVisible(true);

}

public void CloseWindow()
{
this.setVisible(false);
}
public Person SubmitData()
{
String fn = fName.getText();
String ln = lName.getText();
int h = Integer.parseInt(Height.getText());

Person p = new Person(fn, ln, h);
int i = OneMissingFieldException(p);
int j = MultipleMissingFieldException(p);
if(i == 1 && j == 1)
System.out.println(p);
return p;
}


public void OutputList() throws IOException
{
if(listOfObjects.peekFirst()!=null)
{
PrintWriter pw = new PrintWriter( new FileWriter("outputp.txt") );
Object[] pa = listOfObjects.toArray();
pw.println("Person");
for(int x = 0; x<pa.length; x++)
{
pw.println(pa[x]);
pw.println("");
}
pw.close();
}
}

class CancelListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
CloseWindow();
}
}
class SubmitListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {


listOfObjects.add(SubmitData());
CloseWindow();

}
public int OneMissingFieldException(Person p)
{

String fName = ((Person) p).getFName();


try
{
if(fName == "" || fName == null || fName == " ")

throw new MissingFieldException();

}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name." + mfe);
return 0;
}


String lName = ((Person) p).getFName();
try
{
if(lName == "" || lName == null || lName == " ")

throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a last name." + mfe);
return 0;
}
int height = ((Person) p).getHeight();
try
{

if(!(height >= 0) )

throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a height." + mfe);
return 0;
}

return 1;

}


public int MultipleMissingFieldException(Person p)
{
if (p instanceof Person)
{
String fName = ((Person) p).getFName();
String lName = ((Person) p).getFName();
try
{
if((fName == "" || fName == null || fName == " ")&&(lName == "" || lName == null || lName == " "))

throw new MissingFieldException();

}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name or last name." + mfe);
return 0;
}

}
String fName = ((Person) p).getFName();
int height = ((Person) p).getHeight();
try
{
if((fName == "" || fName == null || fName == " ")&&(!(height >=0)))

throw new MissingFieldException();

}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name or height." + mfe);
return 0;
}

String lName = ((Person) p).getFName();

try
{
if((lName == "" || lName == null || lName == " ")&&(!(height >=0)))

throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a last name and height." + mfe);
return 0;
}


return 1;
}



}

最佳答案

我认为你犯了一个相当简单的错误,我已经犯过很多次了。因为“String”变量不是数据类型(例如 int、long 和 double),但实际上是 String 类的对象(由大写首字母表示),因此您必须检查字符串类中的值是否等于通过在要检查的字符串末尾使用 .equals("要检查的字符串值") 函数来检查要检查的值。举个例子,我想弄清楚 myString 是否等于 yourString(具有相同的单词/值)。我会写:

myString.equals(yourString); //which would return a boolean

与你写的相反

myString == yourString; //which does not work

关于java - JTextField无法在if语句java中使用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864280/

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