2011-03-01 4 views
0

以下のコードに関しては、BOOKGUIクラスをBOOKSHELFクラスにアクセスさせてメソッドから値を返そうとしています。 BOOKGUIクラスJava GUIは試行時にエラーを返さない

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 0123で、次のようなものを使用して

BOOKSHELF CLASS

public int sizeOfBookshelf() 
{ 
    return books.size(); 
} 
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でエラーが発生しています。 完全に間違っているのですか?


ご協力いただければ幸いです。おかげ

+0

ドン:あなたは、書籍の数を表示する(コンソールでそれをログに記録する)System.out.println(bookShelf.sizeOfBookshelf())を使用するか、またはあなたのコメントのコードでやっているように、ダイアログボックスに表示するためのJOptionPaneを使用したい場合コード内で 'BookGUI'と呼ばれるときにテキストを実行する際にBOOKGUIと呼ぶべきではありません。 –

答えて

0

あなたはbookShelfオブジェクトにsizeOfBookshelfメソッドを呼び出したい場合は、構文は

bookShelf.sizeOfBookshelf() 

ある、ない

books.size() 

また、actionPerformedの戻り型はvoidです。 voidメソッドからint値を返すことはできません。

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

ありがとう、私はアドバイスとして、私はint値/ voidメソッドの問題を回避する方法をもう1つのこと、私は行を編集しましたか? – Simon

+0

@シモン:私は答えを更新しました –

+0

本当に助けになりましたが、JB Nizetが頭に釘を打つと思います。これはactioneventsなどについて多くのことを学ぶのに役立ちました。 – Simon

1

は、これは宿題のように見えるので、私はあなたにヒントを与えている:)

をあなたのJButtonのアクションのコマンドを設定するための最初の必要性

e.getActionCommand() 

ような何かを行うには:

buttonX.setActionCommand("do something"); 

詳細については、ActionEventおよびsetActionCommand()を参照してください。

+0

ありがとう、はいそれは割り当てであり、私をひっくり返す部分は、私がbookguiクラスと本棚クラスを互いに話し合うことができないように思われることです。 – Simon

+0

残念です。 guiとこのサイトの新機能... – Simon

+0

if(e.getActionCommand()。equals( "BookShelfのサイズ")) \t return books.size();そしてなぜそれがbooks.sizeと「話し合わない」のかというアイデアは、私はそれを理解するのに苦労しているので、どんなpointesも高く評価されるだろう。ありがとう。 – Simon

関連する問題