2016-04-22 44 views
-1

我为我的整个程序设置了结构,但在创建两个类TooColdException和TooHotException时遇到了问题,它们需要一个承包函数,它接受一个String参数并将其传递给Exception类的构造函数,我半创建了类,但不知道如何完成,我的代码在下面,每个secimport java.awt。 ; import java.awt.event。; import javax.swing。*;创建一个类与例外

public class HotCoffeePanel extends JPanel implements ActionListener 
{ 
    private JLabel  label; 
    private JTextField temperature; 
    private JButton check_temp; 

    public HotCoffeePanel() 
    { 
    label  = new JLabel("Water temperature in \u00b0F:"); 
    temperature = new JTextField(4); 
    check_temp = new JButton("Check Temperature"); 

    add(label); 
    add(temperature); 
    add(check_temp); 
    check_temp.addActionListener(this); 

    setPreferredSize(new Dimension(300, 75)); 
    setBackground(Color.yellow); 
    } 

    // ----------------------------------------------------- 
    // Listen for the Check Temperature button and determine 
    // if water is the correct temperature to brew coffee 
    // ----------------------------------------------------- 
    public void actionPerformed(ActionEvent event) 
    { 
    if (Integer.parseInt(temperature.getText()) < 190) 
     try 
     { 
     throwTooColdException(); 
     } 
     catch(TooColdException tce) 
     { 
     JOptionPane.showMessageDialog(null, tce.getMessage()); 
     } 
    else if (Integer.parseInt(temperature.getText()) > 200) 
     try 
     { 
     throwTooHotException(); 
     } 
     catch(TooHotException the) 
     { 
     JOptionPane.showMessageDialog(null, the.getMessage()); 
     } 
    else 
     JOptionPane.showMessageDialog(null, "Water temperature is fine for brewing coffee."); 
    } 

    //------------------------ 
    // TooColdException class 
    //------------------------ 

    public class TooColdException 
    } 
    public TooColdException(String ) 

    } 

    //------------------------ 
    // TooHotException class 
    //------------------------ 

    public class TooHotException 
    } 
    public TooColdException(String ) 

    } 



    // ------------------------------------- 
    // Exception thrown if water is too cold 
    // ------------------------------------- 
    private void throwTooColdException() throws TooColdException 
    { 
    throw new TooColdException("Temperature is too cold to brew coffee."); 
    } 

    // ------------------------------------ 
    // Exception thrown if water is too hot 
    // ------------------------------------ 
    private void throwTooHotException() throws TooHotException 
    { 
    throw new TooHotException("Temperature is too hot to brew coffee."); 
    } 
} // End of HotCoffeePanel class definition 
+0

“我成立了结构在我的整个程序” - 不,你没有。你甚至不打开括号,你关闭它们。尝试阅读关于基本的Java语法。 – f1sh

回答

1

您的例外情况没有正确制定。您需要正确关闭括号。而且,它们必须从Java Exception类继承。

像这样:

public class TooHotException extends Exception { 
    public TooHotException(String message) { 
     super(message); 
    } 
}