0

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



}
4

2 回答 2

2

使用 String 你必须使用string.equals(otherString),而不是string == otherString

于 2013-04-07T15:50:30.837 回答
2

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

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

而不是你写它的方式

myString == yourString; //which does not work
于 2013-04-07T16:05:51.173 回答