[如何通过JScrollpane列表获取Jlist以在JFrame上显示?没有出现

问题描述 投票:0回答:1

我正在使用一个处理书店系统的GUI应用程序处理学校作业,该应用程序应在书店中显示该书的列表,但是当我启动它时,没有任何显示。下面,我将发布正在使用的代码。另外,我只能在TODO标签区域中更改或添加新代码。根据我的理解,book数组存储在bookarraymodel中,其中jlist包含该内容,而jscrollpane内包含多数民众赞成的内容,因此应将其显示。

这里有课本:

/**
 * Book
 */
public class Book {

    public enum BookCategory {
        Programming, Database, Design
    }

    private String title;
    private String authors;
    private int pages;
    private BookCategory category;

    public Book(String title, String authors, int pages, BookCategory category) {
        this.title = title;
        this.authors = authors;
        this.pages = pages;
        this.category = category;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthors() {
        return authors;
    }

    public void setAuthors(String authors) {
        this.authors = authors;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public BookCategory getCategory() {
        return category;
    }

    public void setCategory(BookCategory category) {
        this.category = category;
    }
}

接下来是bookarraymodel用于帮助管理图书数组的方法:

import javax.swing.AbstractListModel;

/**
 * A book array model used by {@link javax.swing.JList}.
 */
public class BookArrayModel extends AbstractListModel<String> {
    private Book[] bookArray;

    public BookArrayModel(Book[] bookArray) {
        this.bookArray = bookArray;
    }

    public void setBookArray(Book[] bookArray) {
        this.bookArray = bookArray;
    }

    @Override
    public int getSize() {
        return bookArray.length;
    }

    @Override
    public String getElementAt(int index) {
        return bookArray[index].getTitle();
    }
}

然后,我们有一个booktorage类,其中存储了book数组,并且有各种方法可以帮助管理它:

/**
 * A collection of {@link Book}.
 */
public class BookStorage {

    private Book[] books = new Book[100];

    public BookStorage() {

    }

    /**
     * Initializes the book storage with some arbitrary book objects.
     */
    public void initBooks() {
        // TODO Add your code here...
        books[0] = new Book("Testing book1", "Jamal", 420, Book.BookCategory.Programming );
        books[1] = new Book("Effective Java", "Joshua Bloch",344,Book.BookCategory.Design);
        books[2]= new Book("Clean Code", "Robert C. Martin",780, Book.BookCategory.Programming);
        books[3] = new Book("Java Concurrency in Practice", "Brian Goetz", 256, Book.BookCategory.Database);
        books[4] = new Book("Head First Design Patterns", "Kathy Sierra", 588, Book.BookCategory.Database);
        books[5]= new Book("Spring in Action", "Criag Walls", 533,Book.BookCategory.Programming);
        books[6] = new Book("Test Driven", "Lasse Koskela",434,Book.BookCategory.Design);
        books[7] = new Book("The Definitive Guide to Java Performance", "Scott Oaks",567,Book.BookCategory.Database );
        books[8] = new Book("Head First Java", "Bert Bates",923,Book.BookCategory.Design );
        books[9] = new Book("Head First Object-Oriented Analysis and Design", "David West",844,Book.BookCategory.Programming);
        books[10] = new Book("Java: A Beginner's Guide", "Herbert Schildt", 255,Book.BookCategory.Programming);




    }


    /**
     * Uses the given book to update the existing book with the same title.
     */
    public void update(Book book) {
        // TODO Add your code here...

    }

    /**
     * Removes a book by title.
     */
    public void remove(String bookTitle) {
        // TODO Add your code here...

    }

    /**
     * Adds a new book.
     */
    public void add(Book book) {
        // TODO Add your code here

    }

    /**
     * Gets a book by title.
     */
    public Book getByTitle(String title) {
        // TODO Add your code here...
   }

    /**
     * Searches for books whose title contains the keyword and returns them ordered by titles (in alphabet order).
     */
    public Book[] titleSearch(String keyword) {
        // TODO Add your code here...

        }


    /**
     * Returns all books sorted by their titles (in alphabet order).
     */
    public Book[] getAll() {
        // TODO Add your code here...
        sortByTitle(books);
        return books;


    }

    /**
     * Sorts an array of books by their titles in alphabet order.
     */
    private Book[] sortByTitle(Book[] bookArray) {
        // TODO Add your code here...
        Book temp;
        for(int i = 1; i < bookArray.length; i++)
        {
          for(int j = i; j > 0; j--)
          {
            if(bookArray[j] == null)
            {
              break;
            }
          else if(bookArray[j].getTitle().charAt(0) <bookArray[j-1].getTitle().charAt(0))
          {
            temp = bookArray[j];
            bookArray[j] = bookArray[j-1];
            bookArray[j=1] = temp;
          }
        }
      }


        return bookArray;
    }

}

最终是启动应用程序的booklistwindow,它设置了GUI:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

/**
 * BookListWindow
 */
public class BookListWindow extends JFrame implements ActionListener {

    //======== Top ========
    private JPanel topPanel;
    private JTextField searchTextField;
    private JButton searchButton;
    private JButton clearButton;

    //======== Middle ========
    private JScrollPane titleListScrollPane;
    private JList<String> bookTitleList;

    //======== Bottom ========
    private JPanel bottomPanel;
    private JButton addButton;
    private JButton detailButton;
    private JButton removeButton;

    //======== Data ========
    private BookStorage bookStorage;
    private BookArrayModel bookListModel;

    public BookListWindow(BookStorage bookStorage) {
        this.bookStorage = bookStorage;
        bookListModel = new BookArrayModel(bookStorage.getAll());
        initComponents();
    }


  //  / * Clears the search results and list all the books.
     //*/
    public void resetToAll() {
        bookListModel.setBookArray(bookStorage.getAll());
        searchTextField.setText("");
        bookTitleList.updateUI();
    }

    /**
     * Returns the book storage.
     */
    public BookStorage getBookStorage() {
        return bookStorage;
    }

    /**
     * Initializes the components.
     */
    private void initComponents() {
        Container contentPane = getContentPane();
        this.setTitle("Book Management");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //======== Top ========
        topPanel = new JPanel();
        searchTextField = new JTextField();
        searchButton = new JButton("SEARCH");
        clearButton = new JButton("CLEAR");

        searchButton.addActionListener(this);
        clearButton.addActionListener(this);

        {
            // Set the layout for topPanel and add the buttons.
            // TODO Add your code here...
            topPanel.setLayout(new GridLayout(1,3));
            topPanel.add(searchTextField);
            topPanel.add(searchButton);
            topPanel.add(clearButton);
        }


        //======== Middle ========
        titleListScrollPane = new JScrollPane();
        bookTitleList = new JList<>();

        {
            // Configure the bookTitleList 1) Use single selection
            //TODO Add your code here...

            bookTitleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        }

        titleListScrollPane.setViewportView(bookTitleList);

        //======== Bottom ========
        bottomPanel = new JPanel();
        addButton = new JButton("ADD");
        detailButton = new JButton("DETAIL");
        removeButton = new JButton("REMOVE");

        addButton.addActionListener(this);
        detailButton.addActionListener(this);
        removeButton.addActionListener(this);

        {
            // Set the layout for bottomPanel and add the buttons.
            // TODO Add your code here...
            bottomPanel.setLayout(new GridLayout(1,3));
            bottomPanel.add(addButton);
            bottomPanel.add(detailButton);
            bottomPanel.add(removeButton);


        }

        contentPane.setLayout(new BorderLayout());
        {
            // Add the components to contentPane with proper layout options.
            // TODO Add your code here...

          contentPane.add(topPanel, BorderLayout.NORTH);
          contentPane.add(titleListScrollPane, BorderLayout.CENTER);
          contentPane.add(bottomPanel, BorderLayout.SOUTH);


        }

        pack();
        setLocationRelativeTo(getOwner());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       if (e.getSource() == searchButton) {
           // Action for the SEARCH button
           // TODO Add your code here...



       } else if (e.getSource() == clearButton) {
           // Action for the CLEAR button
           // TODO Add your code here...
           resetToAll();

       } else if (e.getSource() == addButton) {
           // Action for the ADD button
           // TODO Add your code here...


       } else if (e.getSource() == detailButton) {
           // Action for the DETAIL button
           // TODO Add your code here...


       } else if (e.getSource() == removeButton) {
           // Action for the REMOVE button
           if (!bookTitleList.isSelectionEmpty()) {
               bookStorage.remove(bookTitleList.getSelectedValue());
               JOptionPane.showMessageDialog(this, "Remove Successful!");
               resetToAll();
           }
       }
    }

    public static void main(String[] args) {
        BookStorage bookStore = new BookStorage();
        bookStore.initBooks();
        BookListWindow bookListWindow = new BookListWindow(bookStore);
        bookListWindow.setVisible(true);
    }
}
java swing jscrollpane jlist listmodel
1个回答
0
投票

唯一不让JFrame出现的问题是该代码无法编译。如果实现getByTitle的方法titleSearchBookStorage(以返回有用的内容),则代码将编译并且框架将显示(我对其进行了测试)。现在剩下的是实现分配的所有方法。

© www.soinside.com 2019 - 2024. All rights reserved.