java - 处理 GUI 中的异常时出现问题

标签 java swing

关于以下代码 - 如果在添加书籍时将数字以外的任何内容输入到“成本”中,我试图让它抛出异常,正如你所看到的,我已经采取了一些措施,但我只是想要显示一个消息对话框,指出“请输入一个数字”,而不是出现一个输入对话框,当我尝试更改为消息对话框时,Eclipse 返回错误。 请问有什么建议吗?

相关片段是

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);
        }

    }
    }

最佳答案

当发生异常时,您不应该尝试读取用户在消息对话框中输入的内容:消息对话框并不意味着输入任何内容。这就是为什么 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吗? ?

关于java - 处理 GUI 中的异常时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174308/

相关文章:

java - JButton - 放置图像不起作用

java - JPA 与非 JTA : Is it possible to close EntityTransaction safely without commit?

java - 获取不同代理类型 AnyLogic 的等待时间

Java 哈希集合

java - 使用 Solr 的奇怪行为

java - jlist get 和 set 选择损坏?

java - 如何设置并锁定JScrollpane到底部?

java - 在 jMock 期望中允许 Object 或 null

java - 带有自定义边框的 JFrame 不显示控件

java - JTextArea 中 setText() 的机制?