尝试o时,Java GUI不会返回错误

时间:2011-03-01 11:35:23

标签: java

关于下面的代码,我试图让BOOKGUI类访问BOOKSHELF类并从方法返回值。

BOOKSHELF CLASS

public int sizeOfBookshelf()
{
    return books.size();
}

BOOKGUI

中使用以下内容
JButton button3 = new JButton("Size of BookShelf");
        content.add(button3);
        button3.addActionListener(this);

 if (e.getActionCommand().equals("Size of BookShelf"))
            return books.size();   


BOOKGUI CLASS

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

{

    Book book = new Book("", "", 0, "", 0);
    String title  = "";
    String author  = "";
    int year = 0;
    String publisher  = "";
    double cost = 0;


    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 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(int 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("Title");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);

        JButton button2 = new JButton("Author");
        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");
        book.setTitle(title);
        book.setAuthor(author);
        book.setPublisher(publisher);

            bookShelf.addBook(book);

        // set up each individual title, author, etc.  use joptionpane and mutators?
       //exception handling...
        // add the book to the bookShelf

        String message =  "The title of the book is :" + title + 
        "the Author of the Book is : " + author + " and it's published by " + publisher;
       //TAKE A LOOK AT THIS //JOptionPane.showMessageDialog(
                //null, " The sum is " + sum, "Results",
                //JOptionPane.PLAIN_MESSAGE );

        //System.exit( 0 );
        JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);

        //Container content = getContentPane( );
        if (e.getActionCommand().equals("Size of BookShelf"))
            return books.size();   

            //return sizeofBookshelf();
        //String message = "Number of books in shelf "+books.sizeOfBookshelf();
        //JOptionPane.showMessageDialog(null, message, "Book Shelf", JOptionPane.PLAIN_MESSAGE);

     //sizeOfBookshelf = JOptionPane.showInputDialog("Size");
     //sizeOfBookshelf = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter size of bookshelf")); 

        //label2 = new JButton("Get Book Information");
        //content.add(label2);

        //JButton label3 = new JButton("Show Total Cost of Books");
        //content.add(label3);

    }
}


BOOKSHELF CLASS

import java.util.ArrayList;

/**
  * This class holds an ArrayList of Book.  There are methods for adding Book objects to the ArrayList,
  * calculating the total cost of all books in the ArrayList, determining how many Book objects are
  * in the ArrayList and determining the highest price paid for any one Book.
  *
  * @version 1.0
  */

public class BookShelf
{
    //create an ArrayList of Book.
    public ArrayList<Book> books;

    public BookShelf()
    {    
       books = new ArrayList<Book>();
    }

    /**
     * This method adds a Book object to the ArrayList
     * 
     * @param theBook The book object that will be added to the ArrayList
     * 
     */
    public void addBook(Book theBook)
    {
        books.add(theBook);
    }

    /**
     * This method returns the size of the ArrayList, that is, the number of books
     * that have been added to the ArrayList
     * 
     * @return The size of the ArrayList 
     */
    public int sizeOfBookshelf()
    {
        return books.size();
    }

    /**
     * This method calculates the cost of the book shelf, that is, it totals the 
     * cost of all the books in the ArrayList and returns it.
     * 
     * @return The cost of the book shelf
     */
    public double costOfBookshelf(){

        double total = 0;

        for(Book book : books) {
            total = total + book.getCost();
        }
        return total;
    }


    //create a method called highestPricePAid that will return the cost of the most expensive book in the ArrayList

    /**
     * This method finds the price of the most expensive book in the ArrayList and returns it.
     * 
     * @return The cost of the most expensive book on the book shelf
     */
    public double highestPricePaid(){

       double highestPrice = 0;

       for(Book book : books) {

            if((highestPrice == 0) || (highestPrice < book.getCost())) { 
                highestPrice = book.getCost(); }
       }           
       return highestPrice;
    }             
}

但我在Eclipse中遇到错误。 我完全走错了路吗?

任何协助将不胜感激。感谢

2 个答案:

答案 0 :(得分:1)

这看起来像是家庭作业,所以我给你一个提示:)

做类似

的事情
e.getActionCommand()

首先需要为JButton设置一个操作命令:

buttonX.setActionCommand("do something");

有关详细信息,请参阅ActionEventsetActionCommand()

答案 1 :(得分:0)

如果要在sizeOfBookshelf对象上调用bookShelf方法,则语法为

bookShelf.sizeOfBookshelf()

,而不是

books.size()

此外,actionPerformed的返回类型无效。您不能从void方法返回int值。如果要显示书籍数量,请使用System.out.println(bookShelf.sizeOfBookshelf())(在控制台中记录),或使用JOptionPane将其显示在对话框中,就像您在评论代码中所做的那样:

if (e.getActionCommand().equals("Size of BookShelf")) {
    int sizeOfBookShelf = bookShelf.sizeOfBookshelf();
    String message = "The book shelf has " + sizeOfBookShelf + " book(s)";
    JOptionPane.showMessageDialog(this, message);
}