Java 如何让Jlist和JScrollpane列表显示在JFrame上?isn';不出现
我正在用一个GUI应用程序处理一个图书存储系统,该应用程序应该显示图书存储系统中的图书列表,但当我启动它时,什么也没有显示。下面我将发布我正在使用的代码。此外,我只能在TODO标记区域中更改或添加新代码。据我所知,book数组存储在bookarraymodel中,jlist包含bookarraymodel,它位于添加到contentpane的jscrollpane中,因此它应该显示它 这是图书课:Java 如何让Jlist和JScrollpane列表显示在JFrame上?isn';不出现,java,swing,jscrollpane,jlist,listmodel,Java,Swing,Jscrollpane,Jlist,Listmodel,我正在用一个GUI应用程序处理一个图书存储系统,该应用程序应该显示图书存储系统中的图书列表,但当我启动它时,什么也没有显示。下面我将发布我正在使用的代码。此外,我只能在TODO标记区域中更改或添加新代码。据我所知,book数组存储在bookarraymodel中,jlist包含bookarraymodel,它位于添加到contentpane的jscrollpane中,因此它应该显示它 这是图书课: /** * Book */ public class Book { public e
/**
* 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();
}
}
import javax.swing.AbstractListModel;
/**
*{@linkjavax.swing.JList}使用的图书数组模型。
*/
公共类BookArrayModel扩展了AbstractListModel{
私人书籍[]书籍阵列;
公共BookArrayModel(Book[]bookArray){
this.bookArray=bookArray;
}
公共空白setBookArray(书籍[]书籍数组){
this.bookArray=bookArray;
}
@凌驾
公共int getSize(){
返回bookArray.length;
}
@凌驾
公共字符串getElementAt(int索引){
返回bookArray[index].getTitle();
}
}
然后我们有一个bookstorage类,它存储了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;
}
}
/**
*{@linkbook}的集合。
*/
公营书库{
私人书籍[]书籍=新书[100];
公共图书馆{
}
/**
*使用一些任意图书对象初始化图书存储。
*/
公共图书{
//要在此处添加代码,请执行以下操作。。。
books[0]=新书(“testingbook1”,“Jamal”,420,Book.BookCategory.Programming);
books[1]=新书(“有效Java”,“Joshua Bloch”,344,Book.BookCategory.Design);
图书[2]=新书(“干净的代码”,“罗伯特·C·马丁”,780,图书。图书类别。编程);
books[3]=新书(“实践中的Java并发”,“Brian Goetz”,256,Book.BookCategory.Database);
books[4]=新书(“头先设计模式”,“凯西·塞拉”,588,Book.BookCategory.Database);
图书[5]=新书(“行动中的春天”,“克里格墙”,533,图书。图书类别。编程);
books[6]=新书(“测试驱动”,“Lasse Koskela”,434,Book.BookCategory.Design);
books[7]=新书(“Java性能的权威指南”,“Scott Oaks”,567,Book.BookCategory.Database);
books[8]=新书(“头先Java”,“Bert Bates”,923,Book.BookCategory.Design);
books[9]=新书(“首个面向对象的分析与设计”,“David West”,844,Book.BookCategory.Programming);
books[10]=新书(“Java:初学者指南”,“Herbert Schildt”,255,Book.BookCategory.Programming);
}
/**
*使用给定的书籍更新具有相同标题的现有书籍。
*/
公共作废更新(书籍){
//要在此处添加代码,请执行以下操作。。。
}
/**
*按书名删除一本书。
*/
公共作废删除(字符串书名){
//要在此处添加代码,请执行以下操作。。。
}
/**
*添加一本新书。
*/
公共无效添加(书本){
//TODO在此处添加代码
}
/**
*按书名获取一本书。
*/
公共图书getByTitle(字符串标题){
//要在此处添加代码,请执行以下操作。。。
}
/**
*搜索其标题包含关键字的书籍,并按标题(字母顺序)返回它们。
*/
公共图书[]标题搜索(字符串关键字){
//要在此处添加代码,请执行以下操作。。。
}
/**
*返回按标题排序的所有书籍(按字母顺序)。
*/
公共书籍[]getAll(){
//要在此处添加代码,请执行以下操作。。。
分类(书籍);
还书;
}
/**
*按书名按字母顺序对一系列书籍进行排序。
*/
私人书籍[]分类页(书籍[]书籍数组){
//要在此处添加代码,请执行以下操作。。。
书本温度;
for(int i=1;i0;j--)
{
if(bookArray[j]==null)
{
打破
}
否则如果(bookArray[j].getTitle().charAt(0)唯一不让JFrame
显示的问题是该代码没有编译。如果实现BookStorage
的getByTitle
和titleSearch
方法(返回一些有用的东西),那么代码将编译并显示框架(我测试了一下)。现在剩下的就是实现所有分配的方法。尝试创建一个非常小的程序,在没有所有不必要的方面的情况下展示您的问题。将其设置为一个并发布,而不是您的问题中包含的单独类。人们可能会也可能不会发现您代码中的错误,但人们并不是可以使用c语言的虚拟机运行您的代码片段以查看出了什么问题。
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);
}
}