繁体   English   中英

如何将JList对象添加到BorderLayout类型面板?

[英]How to add JList object to a BorderLayout type Panel?

我在尝试将JList添加到面板的WEST时遇到麻烦。

通过使用另一个面板并将其添加到原始面板,我能够在NORTH上成功添加文本字段和搜索按钮。

JPanel panelShop = new JPanel();

    // Example of items in the scrollbar list
    String[] results = { "Result 1", "Result 2", "Result 3", "Result 4"};
    JList searchList = new JList(results);
    searchList.setLayoutOrientation(JList.VERTICAL);
    // Sets the number of items to display without requiring to scroll
    //searchList.setVisibleRowCount(4);
    panelShop.add(searchList, BorderLayout.WEST);

问题是当我尝试将JList添加到原始面板的WEST边框时,代码的最后一行。

什么都没有出现。

谢谢!

尝试这个

public class TextJpanel extends JFrame {

public TextJpanel() {
    JPanel panelShop = new JPanel();
    panelShop.setLayout(new GridLayout());
    // Example of items in the scrollbar list
    String[] results = { "Result 1", "Result 2", "Result 3", "Result 4" };
    JList searchList = new JList(results);
    searchList.setLayoutOrientation(JList.VERTICAL);
    // Sets the number of items to display without requiring to scroll
    // searchList.setVisibleRowCount(4);
    panelShop.add(searchList, BorderLayout.WEST);
    add(panelShop);
}

public static void main(String[] args) {
    TextJpanel test = new TextJpanel();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setSize(200, 200);
    test.setVisible(true);

}
}

确保如果要为JPanel使用gridlayout,则必须在此面板上设置GridLayout管理器。

暂无
暂无

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

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