GUI と jtextfield を使用してデータを入力するときに空のフィールドを見つけるカスタム例外を作成しようとしています。例外を実際に処理して記述するのはこれが初めてなので、どうすればよいかわかりません。以下のコードは、これまでに把握できたものであり、機能しません。私の質問は、これを完了するために正しい方向に私を向けることができるポインタまたは進歩を誰かが持っているかということです.
import javax.swing.*;
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();
String h = Height.getText();
int h1 = 0;
if(!(h.equals(""))){
h1 = Integer.parseInt(Height.getText());
}
Person p = new Person(fn, ln, h1);
int i = OneMissingFieldException(p);
if(i == 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) extends Exception
{
System.out.println("one");
String fName = ((Person) p).getFName();
String lName = ((Person) p).getFName();
int height = ((Person) p).getHeight();
try
{
System.out.println("two");
if(fName == "" || fName == null || fName == " ");
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name.");
return 0;
}
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;
}
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 Person SubmitData()
{
Person p = null;
try{
String fn = fName.getText();
String ln = lName.getText();
String h = Height.getText();
int h1 = 0;
if(!(h.equals(""))){
h1 = Integer.parseInt(Height.getText());
p = new Person(fn, ln, h1);
checkForMissingFields(p);
}
}
catch(MissingFieldException e){
}
System.out.println(p);
return p;
}
public class MissingFieldException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String args){}
public MissingFieldException() { }
public MissingFieldException( String msg ) {
super( msg );
}
public void checkForMissingFields( Object o ) throws MissingFieldException {
int i =OneMissingFieldException(o);
if(i == 1)
throw new MissingFieldException();
}
public static int OneMissingFieldException(Object o)
{
if (o instanceof Person)
{
String fName = ((Person) o).getFName();
String lName = ((Person) o).getFName();
int height = ((Person) o).getHeight();
if(fName == "" || fName == null || fName == " "){
System.out.println("You did not enter a first name.");
return 1;
}
if(lName == "" || lName == null || lName == " "){
System.out.println("You did not enter a last name.");
return 1;
}
if(height == 0) {
System.out.println("You did not enter a height.");
return 1;
}
return 0;
}
}
}