2016-04-22 15 views
-1

私はプログラム全体の構造を設定しましたが、TooColdExceptionとTooHotExceptionの2つのクラスを作成する際に問題がありましたが、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); 
    } 
} 
関連する問題