开发者

Java GUI won't returns error when attempting o

开发者 https://www.devze.com 2023-02-14 06:25 出处:网络
Regarding the code below, I\'m trying to get the BOOKGUI class to access the BOOKSHELF class and return the value from the method.

Regarding the code below, I'm trying to get the BOOKGUI class to access the BOOKSHELF class and return the value from the method.

BOOKSHELF CLASS

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

by using something like the following in the BOOKGUI class

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

But i'm getting errors in Eclipse. Am i completely on the wrong track ?

Any assistance would be appreciated. thanks


This looks like homework, so I'm giving you a hint :)

To do something like

e.getActionCommand()

you first need to set an action command for your JButton:

buttonX.setActionCommand("do something");

Refer to ActionEvent and setActionCommand() for more information.


If you want to call the sizeOfBookshelf method on the bookShelf object, then the syntax is

bookShelf.sizeOfBookshelf()

, not

books.size()

Also, actionPerformed's return type is void. You can't return an int value from a void method. If you want to display the number of books, use System.out.println(bookShelf.sizeOfBookshelf()) (to log it in the console), or use a JOptionPane to display it in a dialog box, as you're doing in your commented code :

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

精彩评论

暂无评论...
验证码 换一张
取 消