gpt4 book ai didi

java - 如何使用 JScrollpane 列表获取 Jlist 以显示在 JFrame 上?没有出现

转载 作者:行者123 更新时间:2023-12-01 21:12:19 26 4
gpt4 key购买 nike

我正在使用处理图书存储系统的 GUI 应用程序完成一项学校作业,该应用程序应该显示图书存储中的书籍列表,但当我启动它时没有任何显示。下面我将发布我正在使用的代码。另外,我只能在 TODO 标记区域中更改或添加新代码。根据我的理解,书籍数组存储在 jlist 包含的 bookarraymodel 中,并且位于添加到内容 Pane 的 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();
}
}

然后我们有 bookstorage 类,它存储了书籍数组以及帮助管理它的各种方法:

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

}

最后是启动应用程序的图书列表窗口,它设置 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);
}
}

最佳答案

唯一无法显示 JFrame 的问题是该代码无法编译。如果您实现 BookStoragegetByTitletitleSearch 方法(返回有用的内容),那么代码将编译并且框架将显示(我测试过)。现在剩下的就是实现赋值的所有方法。

关于java - 如何使用 JScrollpane 列表获取 Jlist 以显示在 JFrame 上?没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58898300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com