gpt4 book ai didi

java - 设置异常(exception)

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:37 26 4
gpt4 key购买 nike

我无法理解如何执行作业中的以下步骤,我要 instructions

拥有 Exception 父类(super class)构造函数意味着什么?我该把它放在哪里?

这里只是 EmptyFieldException 类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EmptyFieldException

{
EmptyFieldException()
{



}


}

这是我的应用程序,大部分工作都已完成

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

public class AccountApplet extends JApplet implements ActionListener
{
// For West
public JLabel ai = new JLabel("Account ID ");
public JTextField aitf = new JTextField();
public JLabel ab = new JLabel("Account Balance ");
public JTextField abtf = new JTextField();

// For East
public JButton dp = new JButton ("Deposit");
public JTextField dptf = new JTextField();
public JButton wt = new JButton ("Withdraw");
public JTextField wttf = new JTextField();

// For South
public JLabel status = new JLabel("placeholder");


public void init()
{
this.setSize(400, 90);

//----------------------
// Set up the Structure
//----------------------

Container c = getContentPane();
JPanel b = new JPanel(new BorderLayout());
JPanel west = new JPanel(new GridLayout(2,2));
JPanel east = new JPanel(new BorderLayout());
JPanel depo_with = new JPanel(new GridLayout(2,2));



// Add BorderLayout to the container
c.add(b);

// Add everything to West
b.add(west, BorderLayout.WEST);


west.setBorder(new TitledBorder("Display Account Information"));
west.add(ai);
west.add(aitf);
aitf.setEditable(false);
west.add(ab);
west.add(abtf);
abtf.setEditable(false);

// Add everything to EAST
b.add(east, BorderLayout.EAST);

east.setBorder(new TitledBorder("Deposit or Withdrawl Funds"));

east.add(depo_with, BorderLayout.EAST);

depo_with.add(dptf);
depo_with.add(dp);
depo_with.add(wttf);
depo_with.add(wt);

dp.addActionListener(this);
wt.addActionListener(this);

// Add everything to EAST
b.add(status, BorderLayout.SOUTH);






} // End intit

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == dp) // Executes if deposit was clicked
{
}

if (e.getSource() == wt) // Executes if withdraw was clicked
{
}
} // End actionPerformed

public void refreshFields()
{
// diplays accound id and balance in left text fields
//should be called when the applet is first displayed and after each valid transaction
}

public double getAmount(JTextField tf) //throws EmptyFieldException,
// NumberFormatException,
// NegativeAmountException
{
return 5.0;
} // End getAmount


} // End Class

最佳答案

调用父类(super class)的构造函数是通过 super 关键字完成的:

... pass a message to the Exception superclass constructor.

public class MyException extends Exception { // Exception is the superclass

public MyException() {
super("My Message"); // call Exception's constructor
}

}

在调用类构造函数时,总是会调用父类(super class)构造函数(Object 除外)。

不过,如果父类(super class)构造函数没有参数,则不必显式调用 super()

还应该注意的是,对父类(super class)构造函数的调用必须始终是构造函数中的第一条语句。

关于java - 设置异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867612/

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