基本上,我的项目是创建一个简单的库系统,其中任何给定的用户一次最多可以有三本书。我使用BreezySwing(因为我需要)并使用JList列出当前取出的书籍。使用新输入更新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的正常工作。剩下的我可以完成。谢谢。
*“与BreezySwing一起做”*唯一确定的方法它不是该API的错误,是准备一个纯J2SE的SSCCE。 –