0

在下图中,底部JComponent是 a 中的JList(列表)JScrollPane。默认情况下,可见行数是 8 或 10,我不知道。但是突然间,当我对项目进行更多更改时,它变成了这个,现在我一次只能看到一个项目(fsgisfg)。如何更改一次显示的行数?

list.setVisibleRowCount(8)不起作用。

使用list扩展AbstractListModel作为模型。

主菜单JFrame使用GridBagLayout.

我不知道是什么可能导致这种情况发生,因为我什至尝试撤消项目中所做的所有更改,结果仍然相同。

private JList<String> list;
[...]

list = new JList<String>(); 
list.setVisibleRowCount(8); //doesn't change anything
[...]

JScrollPane scroll2 = new JScrollPane(list);
[...]

list.setModel(new BookListModel(library));
list.repaint(); //the model gets the data for the list, and refresh is needed

[...]

GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
frame.add(scroll2, c);

图片 http://dl.dropbox.com/u/71389667/problem.jpg

4

2 回答 2

0

嗯...现在很难找出导致问题的原因,所以首先我建议您在继续查找错误之前阅读本教程...

至于这一刻,我可能只是假设问题实际上可能来自布局管理器或这个方向的东西......尝试围绕 JList 包含面板(或您使用的组件......)布局管理器。似乎现在您正在使用某种绝对的,因此例如将其更改为 FlowLayout。

作为另一个提示,检查 ListModel 是否添加了空元素?

有关更多建议,很高兴看到更多代码......


报告是否有帮助

于 2013-01-09T04:52:14.403 回答
0

我想我已经找到了问题,但我不知道是什么原因造成的:首先,我的书以 xml 文件保存在磁盘上的一个文件夹(称为 Library)中;现在如果我删除这个文件夹,以便启用按钮创建库,JList 似乎是正常大小(显示 8 行)。即使我添加书籍,JList 保持不变,这很好。当我尝试实际更改已创建的 xml 文件时出现问题,因为默认情况下该文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<book name="name" author="author" isbn="isbn" />`

如果我做到这一点

<?xml version="1.0" encoding="UTF-8"?>
<book name="name" author="" author="" >
<chapter title="chapter1">
    <paragraph>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
    </paragraph>

</chapter>
</book>

JList 显示搞砸了。

如果我这样做:

<?xml version="1.0" encoding="UTF-8"?>
<book name="fadsf" author="" isbn="" >
    <chapter title="chapter1">
         <paragraph>
            <sentence>sentence1</sentence>
            <sentence>sentence1</sentence>
            <sentence>sentence1</sentence>
    </paragraph>
    </chapter>
</book>

它工作正常。如果我添加第四句标签,事情就会变得丑陋。

于 2013-01-09T10:23:54.037 回答