简体   繁体   English

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

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

I'm having trouble trying to add a JList to the WEST side of my panel. 我在尝试将JList添加到面板的WEST时遇到麻烦。

I was able to successfully add a text field and search button on the NORTH by using another panel and adding it to the original panel. 通过使用另一个面板并将其添加到原始面板,我能够在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);

The problem is the last line of code, when I'm trying to add the JList to the WEST border of the original panel. 问题是当我尝试将JList添加到原始面板的WEST边框时,代码的最后一行。

Nothing shows up. 什么都没有出现。

Thanks! 谢谢!

try this 尝试这个

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

}
}

make sure if you want to use the gridlayout for a JPanel then u have to set a GridLayout manager to this panel. 确保如果要为JPanel使用gridlayout,则必须在此面板上设置GridLayout管理器。

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

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