2011-03-02 64 views
0

關於下面的代碼 - 我試圖讓它拋出並且異常,如果在添加書籍時將數字以外的任何內容輸入到「成本」中,您可以看到我已經得到了一些這樣做,但我只想顯示一個消息對話框,指出「請輸入一個數字」,而不是出現輸入對話框,當我嘗試更改消息對話框時,Eclipse會返回錯誤。 有什麼建議嗎?在GUI中處理異常的問題

相關的片段是

do{ 
     try { 
      cost = Double.parseDouble(JOptionPane.showInputDialog("Cost")); 
      book.setCost(cost); 
      goodInput = true; 
      } 
      catch (NumberFormatException nfe){ 
      cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again" 
      )); 
      } 
     }while (!goodInput); 

,這裏是完整的代碼。謝謝。

import java.awt.FlowLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

import java.awt.event.ActionListener; 
import java.util.ArrayList; 

public class BookGUI extends JFrame implements ActionListener 

{ 

    //String addBook=""; 
    // public ArrayList<Book> books; 

    //Book books = new Book ("", "", 0, "", 0); 
    Book book = new Book("", "", 0, "", 0); 
    String title = ""; 
    String author = ""; 
    int year = 0; 
    String publisher = ""; 
    double cost = 0; 
    double total = 0; 
    boolean goodInput = false; 


    public BookShelf bookShelf = new BookShelf(); 
    public static final int WIDTH = 300; 
    public static final int HEIGHT = 200; 

    //Creates & displays a window of the class FlowLayoutDemo 
    public static void main(String[] args) 
    { 
     BookGUI gui = new BookGUI(); 
     gui.setVisible(true); 
    } 

    // public String getTitle() 
    // { 
    // return title; 
    //} 

    public void setTitle(String title) //this is relevant 
    { 
     this.title = title; 
    } 

    public void setAuthor(String author) //this is relevant 
    { 
     this.author = author; 
    } 

    public void setYear(int year) //this is relevant 
    { 
     this.year = year; 
    } 
    public void setPublisher(String publisher) //this is relevant 
    { 
     this.publisher = publisher; 
    } 

    public void setCost(double cost) //this is relevant 
    { 
     this.cost = cost; 
    } 

    public BookGUI() 
    { 

     setSize(WIDTH, HEIGHT); 
     addWindowListener(new WindowDestroyer()); 
     setTitle("GUI Assignment"); 
     Container content = getContentPane(); 

     content.setLayout(new FlowLayout()); 

     JButton button1 = new JButton("Hightest Price Paid"); 
     content.add(button1); 
     button1.addActionListener(this); 
     //contentPane.add(button1); 

     JButton button2 = new JButton("Cost of BookShelf"); 
     content.add(button2); 
     button2.addActionListener(this); 

     JButton button3 = new JButton("Size of BookShelf"); 
     content.add(button3); 
     button3.addActionListener(this); 

     JButton button4 = new JButton("Add Book"); 
     content.add(button4); 
     button4.addActionListener(this);  

    } 

    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getActionCommand().equals("Add Book")) 
     //book = JOptionPane.showInputDialog("Add Book"); 
     {  //set up the book object with all the data passed in 
     title = JOptionPane.showInputDialog("Title"); 
     author = JOptionPane.showInputDialog("Author"); 
     publisher = JOptionPane.showInputDialog("Publisher"); 
     //cost = JOptionPane.showInputDialog("Cost"); 
     //cost = Double.parseDouble(JOptionPane.showInputDialog("Cost")); 
     do{ 
     try { 
      cost = Double.parseDouble(JOptionPane.showInputDialog("Cost")); 
      book.setCost(cost); 
      goodInput = true; 
      } 
      catch (NumberFormatException nfe){ 
      cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again" 
      )); 
      } 
     }while (!goodInput); 


     book.setTitle(title); 
     book.setAuthor(author); 
     book.setPublisher(publisher); 
     bookShelf.addBook(book); 

     String message = "The title of the book is :" + title + 
     "the Author of the Book is : " + author + " and it's published by " + publisher + "and it costs" + cost + "euro"; 
     JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE); 
     } 
     else if (e.getActionCommand().equals("Size of BookShelf")) { 
      int sizeOfBookShelf = bookShelf.sizeOfBookshelf(); 
      String message = "The book shelf has " + sizeOfBookShelf + " book(s)"; 
      JOptionPane.showMessageDialog(this, message); 
     } 
     else if (e.getActionCommand().equals("Cost of BookShelf")) 
     { 
      double costOfBookshelf = bookShelf.costOfBookShelf(); 
      String message = "The book shelf value is " + total + costOfBookshelf + "Euro"; 
      JOptionPane.showMessageDialog(this, message); 
     } 

    } 
    } 
+0

你正在得到什麼錯誤? – msarchet 2011-03-02 22:28:48

回答

2

你不應該讀什麼用戶在消息對話框中輸入,當發生異常:消息對話框並不意味着輸入任何東西。這就是爲什麼showMessageDialog返回void,而不是像showInputDialog那樣的String。更換

catch (NumberFormatException nfe){ 
     cost = Double.parseDouble(JOptionPane.showInputDialog ("Numerical entry expected. Please try again" 
     )); 

catch (NumberFormatException nfe){ 
     JOptionPane.showMessageDialog(this, "Numerical entry expected. Please try again"); 

此外,請注意,你應該總是通過父組件在showXXXDialog,爲了對話框是模態(即阻止訪問)的框架這個組件。 你讀過javadoc of JOptionPane嗎?你讀過tutorial about dialogs嗎?

+0

謝謝。不,我沒有閱讀過這個教程,但我現在會,謝謝。最後一個問題請問,numberformat異常的字符串是什麼,以便在輸入數字而不是字符時拋出錯誤? – Simon 2011-03-02 23:12:40

+0

沒有。你必須分析字符串並確定它是否有效。你可以用正則表達式來做到這一點。閱讀http://download.oracle.com/javase/tutorial/essential/regex/ – 2011-03-02 23:15:01

+0

感謝您的幫助,非常感謝。 – Simon 2011-03-02 23:19:27