我目前正在处理需要我创建自定义缺失字段异常的任务。我必须从 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;
}
}