在JPanel中显示JList的错误

时间:2011-12-23 16:49:52

标签: java swing jpanel jlist

以下是我创建JPanel

的代码
public class ListBranchesInterface extends JPanel {
private Library theLibrary; // reference to back end
private ArrayList<LibraryBranch> branches;
private DefaultListModel dlm;
private JList list;
private JScrollPane scroll;

public ListBranchesInterface(Library theLibrary) {
    this.theLibrary = theLibrary;
    branches = new ArrayList<LibraryBranch>();
    branches.addAll(theLibrary.getLibraryBranches());
    System.out.println(branches.size());
    Iterator<LibraryBranch> iter = branches.iterator();
    dlm = new DefaultListModel();
    while (iter.hasNext()) {

        dlm.addElement(iter.next().toString());
        System.out.println("Added");

    }
    list = new JList(dlm); // create a JList from the default list model
    scroll = new JScrollPane(list); // add a scroll pane to the JList
    add(scroll);
    setSize(400, 400);
    setVisible(true);

}
}

当我将面板添加到JFrame时,不会显示任何内容。 (错误不在于将其添加到JFrame,因为我正在添加其他面板并且它们正常工作。)错误是什么?

0 个答案:

没有答案