我正在尝试编写一个自定义异常,在使用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;
}
}
}
答案 0 :(得分:0)
您在下面的行中混淆了方法和类。
public int OneMissingFieldException(Person p) extends Exception
{ ... }
您需要两种不同的结构。
这是一个基本的自定义异常类:
public static class MissingFieldException extends Exception {
public MissingFieldException() { }
public MissingFieldException( String msg ) {
super( msg );
}
}
这是一个可以抛出异常的方法:
public void checkForMissingFields( Person p ) throws MissingFieldException {
...
}
您可以在不检查返回值的情况下调用此方法。 的例外情况。您可以检测何时使用catch块抛出异常。
public Person submitData() {
Person p = null;
try {
p = ...;
checkForMissingFields( p );
...
}
catch ( MissingFieldException e) {
// handle exception
}
return p;
}