基本上,我的项目是创建一个简单的图书馆系统,任何给定的用户一次最多可以拥有三本书。我正在使用 BreezySwing(因为我必须这样做)并使用 JList 列出当前取出的书籍。
要归还书,用户在列表中单击要归还的书,然后单击“归还”按钮。但是,我不知道如何让 JList 正常工作。
如何将新信息添加到列表中?
这是我到目前为止的代码:
import java.util.*;
import javax.swing.*;
import BreezySwing.*;
public class ClientGUI extends GBFrame {
    private JButton returnBook, takeOutBook;
    private JLabel title, author;
    private JTextField titleInput, authorInput;
    private int bookCount = 0;
    DefaultListModel books = new DefaultListModel();
    private JList booksOut = new JList(books);
    public ClientGUI(){
        title = addLabel("Title: ",1,1,1,1);
        author = addLabel("Author: ",2,1,1,1);
        titleInput = addTextField("",1,2,2,1);
        authorInput = addTextField("",2,2,2,1);
        returnBook = addButton("Return",3,1,1,1);
        takeOutBook = addButton("Take Out",3,2,1,1);
        booksOut = addList(4,1,2,1);
        returnBook.setEnabled(false);
        books.addElement("Book - Author");
    }
    public void buttonClicked(JButton buttonObj){
        if(buttonObj == takeOutBook){
            if(bookCount < 3){
                String titleAdd = titleInput.getText();
                String authorAdd = authorInput.getText();
                books.addElement(titleAdd + "By: " + authorAdd);
                bookCount++;
                if(bookCount == 0){
                    takeOutBook.setEnabled(true);
                    returnBook.setEnabled(false);
                } else if(bookCount == 3){
                    takeOutBook.setEnabled(false);
                    returnBook.setEnabled(true);
                } else {
                    takeOutBook.setEnabled(true);
                    returnBook.setEnabled(true);
                }
            } else if(bookCount == 3){
                JOptionPane.showMessageDialog(null, "Please return a book first", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if(buttonObj == returnBook){
            //int n = (Integer) books.getSelectedValue();
            //books.remove(n);
            bookCount--;
            if(bookCount == 0){
                takeOutBook.setEnabled(true);
                returnBook.setEnabled(false);
            } else if(bookCount == 3){
                takeOutBook.setEnabled(false);
                returnBook.setEnabled(true);
            } else {
                takeOutBook.setEnabled(true);
                returnBook.setEnabled(true);
            }
        }
    }
    public static void main(String args[]){
        ClientGUI GUI = new ClientGUI();
        GUI.setSize(300,275);
        GUI.setTitle("Library");
        GUI.setVisible(true);
     }
}
基本上,我需要做的是让 JList 正常工作。剩下的我可以完成。谢谢。
