2013-03-07 76 views
0

我是Java的新手,並且一直在研究一個轉換器,它將採用一個十進制數字並將其轉換爲二進制,反之亦然。我想實現異常處理,但是我很難完全理解這個概念。我希望程序能夠抓到一個NumberFormatException並拋出我在一個單獨的類中創建的NotInBinaryException。我能夠拋出新的異常,但不清楚如何成功捕獲異常以顯示將在程序上顯示的JOptionPane,提示用戶以二進制格式輸入數字,同時清除錯誤並將焦點置於文本上字段存在錯誤。到目前爲止,這是我創建的代碼。我很感激任何幫助讓我回到正軌。拋出你自己的例外

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 


public class ImprovedBaseGui extends JFrame implements ActionListener 
{ 
private JTextField txtBaseTen; 
private JTextField txtBaseTwo; 
private JButton btnBaseTen; 
private JButton btnBaseTwo; 
private JButton btnClear; 
public ImprovedBaseGui() 
{ 
    this.setTitle("Base 10/2 Converter");  
    Container canvas = this.getContentPane(); 

    canvas.add(createCenterPanel(), BorderLayout.CENTER); 
    canvas.add(createSouthPanel(), BorderLayout.SOUTH); 


    this.setResizable(false); 
    this.setSize(475, 150); 
    this.setLocation(800, 500); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); 
} 

private JPanel createSouthPanel() 
    { 
     JPanel pnlSouth = new JPanel(); 

     btnBaseTen = new JButton("Base 10"); 
     btnBaseTen.addActionListener(this); 
     btnBaseTen.setToolTipText("Use to convert Base 2 to Base 10"); 
     btnBaseTen.setBackground(Color.CYAN); 
     pnlSouth.add(btnBaseTen); 

     btnBaseTwo = new JButton("Base 2"); 
     btnBaseTwo.addActionListener(this); 
     btnBaseTwo.setToolTipText("Use to convert Base 10 to Base 2"); 
     btnBaseTwo.setBackground(Color.YELLOW); 
     pnlSouth.add(btnBaseTwo); 

     btnClear = new JButton("Clear"); 
     btnClear.addActionListener(this); 
     btnClear.setBackground(Color.RED); 
     pnlSouth.add(btnClear); 

     return pnlSouth; 
    } 

private JPanel createCenterPanel() 
    { 
     JPanel pnlCenter = new JPanel(); 
     pnlCenter.setLayout(new GridLayout(2,2)); 

     pnlCenter.add(wrapMeInAPanel(new JLabel ("Base 10"))); 
     txtBaseTen = new JTextField(16); 
     txtBaseTen.setBackground(Color.YELLOW); 
     pnlCenter.add(wrapMeInAPanel(txtBaseTen)); 

     pnlCenter.add(wrapMeInAPanel(new JLabel("Base 2"))); 
     txtBaseTwo = new JTextField(16); 
     txtBaseTwo.setBackground(Color.CYAN); 
     pnlCenter.add(wrapMeInAPanel(txtBaseTwo)); 

     return pnlCenter; 
    } 

    public static void main(String[] args) 
    { 
     new ImprovedBaseGui(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
     { 

      if(e.getSource() == btnClear) 
      { 
       txtBaseTen.setText(""); 
       txtBaseTwo.setText(""); 
      } 
      if(e.getSource() == btnBaseTwo) 
      { 
       try 
        { 
txtBaseTwo.setText(Integer.toBinaryString(Integer.parseInt(txtBaseTen.getText()))); 
        } 
       catch(NumberFormatException err) 
        { 
JOptionPane.showMessageDialog(this, txtBaseTen.getText()+""); 
        txtBaseTen.setText(""); 
        txtBaseTen.grabFocus(); 
        } 

      } 
      if(e.getSource() == btnBaseTen) 
      { 
       try 
        { 
txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2))); 
      } 
      catch(NumberFormatException err) 
      { 
      throw new NotInBinaryException(); 
       } 

      } 

     } 

    private JPanel wrapMeInAPanel(Component c) 
      { 
       JPanel panel = new JPanel(); 
       panel.add(c); 
       return panel; 
      } 
} 

回答

1

您應該只拋出NotInBinaryException如果你的方法不能從二進制解析處理NumberFormatException。但它可以處理它,你應該處理它。這裏不需要你的NotInBinaryException

只要處理NumberFormatException類似於您在txtBaseTen的情況下如何處理它,儘管您可能希望爲這兩種情況選擇更加用戶友好的錯誤消息。

try 
{ 
    txtBaseTen.setText(Integer.toString(Integer.parseInt(txtBaseTwo.getText(), 2))); 
} 
catch(NumberFormatException err) 
{ 
    JOptionPane.showMessageDialog(this, txtBaseTwo.getText()+""); 
    txtBaseTwo.setText(""); 
    txtBaseTwo.grabFocus(); 
} 
+0

感謝您的意見。您提供的解決方案是我最初編寫代碼的方式。然而爲了學習拋出我自己的異常,我想知道如何完成創建我自己的異常並讓NumberFormatException拋出它。 – 2013-03-07 19:29:11

0

如果你堅持要用NotInBinaryException,則有另一種方法執行轉換爲你,有它拋出一個NotInBinaryException。如果您的NotInBinaryExceptionRuntimeException,則throws子句是不必要的。

private String getDecimalText(String binaryText) throws NotInBinaryException 
{ 
    try 
    { 
     return Integer.toString(Integer.parseInt(decimalText, 2)); 
    } 
    catch (NumberFormatException err) 
    { 
     throw new NotInBinaryException(); 
    } 
} 

然後在您的actionPerformed方法中找到它。

try 
{ 
    txtBaseTen.setText(getDecimalText(txtBaseTwo.getText())); 
} 
catch(NotInBinaryException err) 
{ 
    JOptionPane.showMessageDialog(this, "Number entered was not in binary: " + txtBaseTwo.getText()); 
    txtBaseTwo.setText(""); 
    txtBaseTwo.grabFocus(); 
} 
+0

感謝您的幫助。你的建議很有意義。我試圖做到這一點,而不使用一種方法來處理轉換,它只是沒有工作。我想我太過於這麼想了,因爲現在看起來似乎很簡單。 – 2013-03-07 19:53:56