将GUI功能添加到Java类

时间:2011-02-07 21:55:30

标签: java user-interface

我有一个java类,我想添加一些gui功能,以便我可以返回书名或成本,我知道我需要实现动作听众但我不知道从哪里开始,任何指针将非常感激。代码如下 -

import java.text.*;

public class Book
{

    public static void main (String[] args)
    {

    }

    // Instance variables
    private String title, author, publisher;
    private int year; 
    private double cost;

    /**
     * Book constructor that initialises the instance variables based on user input.
     * 
     * @param title     The title of the book
     * @param author    The author of the book
     * @param year      The year the book was published
     * @param publisher The name of the publisher of the book
     * @param cost      The cost of the book
     */
    public Book(String title, String author, int year, String publisher, double cost)
    {
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        this.cost = cost;
    }

    /**
     * Accessor for the title of the book.
     * @return the title of the book.
     */
    public String getTitle()
    {
        return title;
    }

    /**
     * Accessor for the author of the book.
     * @return the author of the book.
     */
    public String getAuthor() 
    {
        return author;
    }

    /**
     * Accessor for the year the book was published.
     * @return the year the book was published.
     */    
    public int getYear() 
    {
        return year;
    }

    /**
     * Accessor for the publisher of the book.
     * @return the publisher of the book.
     */    
    public String getPublisher() 
    {
        return publisher;
    }

    /**
     * Accessor for the cost of the book.
     * @return the cost of the book.
     */    
    public double getCost() 
    {
        return cost;
    }

    /**
     * Mutator for updating the title of the book.
     * @param title The new title of the book.
     */
    public void setTitle(String title) 
    {
        this.title = title;
    }

    /**
     * Mutator for updating the author of the book.
     * @param author The new author of the book.
     */
    public void setAuthor(String author) 
    {
        this.author = author;
    }

    /**
     * Mutator for updating the year of the book.
     * @param year The new year of the book.
     */
    public void setYear(int year)
    {
        this.year = year;
    }

    /**
     * Mutator for updating the publisher of the book.
     * @param publisher The new publisher of the book.
     */    
    public void setPublisher(String publisher)
    {
        this.publisher = publisher;
    }

    /**
     * Mutator for updating the cost of the book.
     * @param cost The new cost of the book.
     */    
    public void setCost(double cost)
    {
        this.cost = cost;
    }


    /**
     * The standard toString method that returns the details of the book 
     * @return the details of the book formatted as a String.
     */
    public String toString()
    {
        return "The details of the book are: " + title + 
               ", " + author + ", " + year + ", " + publisher + ", " + cost;
    }

}

修改

嗨,这里是第二类是书籍的存储 - 我认为我需要创建一个类似于第一部分,这将会处理GUI元素,我知道我认为我需要在两个类别的书中添加行动聆听者书架和第三类将与显示器交易,但我正在开始麻烦。一些帮助使它开始并解释显示类将如何与书中的方法“对话”并且书架将被真正地归于一致。感谢,回答'问题'问题我正在使用这个项目的最大问题是获得图形用户界面和听众部分!

import java.util.ArrayList;

public class BookShelf
{
    //create an ArrayList of Book.
    private 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;
    }

}

2 个答案:

答案 0 :(得分:2)

您应该看一下本教程,开始使用Java构建图形用户界面:

http://download.oracle.com/javase/tutorial/uiswing/

答案 1 :(得分:1)

我认为在创建GUI之前,您需要首先创建至少一个其他非GUI类,一个处理书籍集合的类,例如类库或BookStore,具体取决于您的需要,所有必要的功能。然后我会使用该类作为GUI的核心或“模型”。

否则,如果没有更具体的问题,很难为您提供有关如何创建GUI的具体建议。您完成此计划/作业的最大问题究竟是什么?此外,与任何编程逻辑一样,在编码之前进行规划,一次解决一个小步骤。