【问题标题】:How to add JList object to a BorderLayout type Panel?如何将 JList 对象添加到 BorderLayout 类型的面板?
【发布时间】:2013-11-28 05:06:26
【问题描述】:

我在尝试将 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 边框时。

什么都没有。

谢谢!

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 我把它缩短...这样更好吗?
  • 它不是自编译的。
  • 我应该如何让你编译我的代码而不在这里发布长代码?
  • 去掉所有多余的废话。我可以将列表添加到组件中,将其放入面板中,然后在 20 LOC 内将其显示在屏幕上。

标签: java swing layout jpanel jlist


【解决方案1】:

试试这个

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 管理器。

【讨论】:

    猜你喜欢
    • 2012-03-19
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多