繁体   English   中英

使用新输入更新JList

[英]Update a JList with new inputs

基本上,我的项目是创建一个简单的图书馆系统,其中任何给定的用户一次最多可以拥有三本书。 我正在使用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正常工作。 剩下的我可以完成。 谢谢。

试试DefaultListModel.addElement

它将触发更新事件以使列表响应

UPDATE

不知道BreezySwing的工作原理,我会专注于这条线...

booksOut = addList(4, 1, 2, 1);

鉴于我相信代码中的其他行正在执行的操作,这似乎是“创建”一个新的Jlist并返回它(应用布局)。

这意味着booksOut使用的模型不再是books模型。

尝试添加

booksOut.setModel(books);

addList行之后,看看是否有帮助

这是一个“空结果”,您可以使用它直到崩溃为止。

扩充清单

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExpandableList {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JPanel gui = new JPanel(new BorderLayout());

                String[] listValues = {
                        "Click", "To", "Add", "New", "Values"
                };
                final DefaultListModel model = new DefaultListModel();
                for (String s : listValues) {
                    model.addElement(s);
                }
                JList list = new JList(model);
                MouseListener addListener = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent me) {
                        String s = JOptionPane.showInputDialog(
                                gui, "New text..");
                        if (s!=null) {
                            model.addElement(s);
                        }
                    }
                };
                list.addMouseListener(addListener);
                gui.add(new JScrollPane(list));

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM