设置 JList 以填充其所添加到的组件的整个大小

发布于 2024-12-23 00:15:45 字数 1079 浏览 1 评论 0原文

下面是我创建 JList 的代码。当它在独立的 JFrame 中创建时,JList 会填充整个框架。但是,当我将其创建为 JPanel 并将其添加到 JFrame 时,它​​没有填充组件的大小,为什么?

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());

    Iterator<LibraryBranch> iter = branches.iterator();
    dlm = new DefaultListModel();
    while (iter.hasNext()) {

        dlm.addElement(iter.next().toString());

    }
    list = new JList(dlm); // create a JList from the default list model

    scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // add a scroll pane
                                                        // to the JList

    add(scroll);
    setVisible(true);

}

below is my code for creating the JList. When it is created in a standalone JFrame the JList fills the entire frame. However, when I create it as a JPanel and add it to the JFrame it is not filling the size of the component, Why?

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());

    Iterator<LibraryBranch> iter = branches.iterator();
    dlm = new DefaultListModel();
    while (iter.hasNext()) {

        dlm.addElement(iter.next().toString());

    }
    list = new JList(dlm); // create a JList from the default list model

    scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // add a scroll pane
                                                        // to the JList

    add(scroll);
    setVisible(true);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-12-30 00:15:45

因为 JFrame内容窗格 的默认布局管理器是 BorderLayout,直接添加到它会填充整个可用空间(即中心) 框架的内容窗格。另一方面,JPanel 的默认布局管理器是 FlowLayout。 FlowLayout 类将组件排成一行,并按其首选大小调整大小。因此,将 JList 添加到 JPanel 不会填充整个可用空间。

Because the default layout manager of a JFrame's content pane is BorderLayout and adding directly to it would fill the entire available space (i.e., center) of the frame's content pane. On the other hand, the default layout manager of a JPanel is FlowLayout. The FlowLayout class puts components in a row, sized at their preferred size. So adding the JList to a JPanel would not fill the entire available space.

江城子 2024-12-30 00:15:45

我从@kavka 得到了答案......事实是更改边框布局。

JPanel As_list_panel = new JPanel();
As_list_panel.setBounds(0, 0, 200, 200);    //I think this line is unecessary. check it                                      
As_list_panel.setLayout(new BorderLayout(0, 0));

From @kavka I derived the answer... The fact is Change to Border Layout.

JPanel As_list_panel = new JPanel();
As_list_panel.setBounds(0, 0, 200, 200);    //I think this line is unecessary. check it                                      
As_list_panel.setLayout(new BorderLayout(0, 0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文