[英]Setting up exceptions
擁有Exception超類構造函數意味着什么? 我會把它放在哪里?
這里只是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
關鍵字調用超類的構造函數:
...將消息傳遞給Exception超類構造函數。
public class MyException extends Exception { // Exception is the superclass
public MyException() {
super("My Message"); // call Exception's constructor
}
}
調用類構造函數時總是調用超類構造函數(可能除了Object
)。
但是,如果超類構造函數沒有參數,則不必顯式調用super()
。
還應該注意,對超類構造函數的調用必須始終是構造函數中的第一個語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.