有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JList左附加并填充

    setLayout(new BorderLayout());
    add(listScroller, BorderLayout.WEST);

当我这样做的时候,我的列表被留下并填充了所有列的大小。但是当我添加更多的东西时

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    gc.gridx = 2;
    gc.gridy = 0;
    add(nameLabel, gc);

它变小了,当我调整大小时,它就变得看不见了。我如何适应左边的列表,右边的其他东西

额外问题:如果列表中的元素很少,我的滚动条不会显示。不管发生什么,我都想看看滚动条


共 (1) 个答案

  1. # 1 楼答案

    首先,您可能想使用一个布局管理器

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    
    gc.gridx = 0;
    gc.gridy = 0;
    gc.weighty = 1;
    gc.fill = GridBagConstraints.BOTH;
    gc.gridheight = GridBagConstraints.REMAINDER;
    add(listScroller, gc);
    
    gc.gridx = 2;
    gc.gridy = 0;
    gc.weighty = 0;
    gc.fill = GridBagConstraints.NONE;
    gc.gridheight = 1;
    add(nameLabel, gc);
    

    或者

    setLayout(new BorderLayout());
    add(listScroller, BorderLayout.WEST);
    add(nameLabel);
    

    取决于你想要实现什么

    有关更多详细信息,请参见Laying Out Components Within a Container

    Extra question: If there is few elements in list, my scroller doesn't show. I wanna see scroller whatever happens.

    你会想看看^{}^{}