如何指定Jlist宽度? [英] How to specify Jlist width?

查看:270
本文介绍了如何指定Jlist宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定我的JList的固定宽度

I want to specify fixed width of my JList

我按照 Gilbert Le Blanc 22k

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
        Panel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        getContentPane().add(panel);

        String [] queries = {"a", "b", "a", "b" , "a", "b", "a", "b"};
        panel.setLayout(new BorderLayout());
        JList list = new JList(queries);
        list.setMaximumSize(new Dimension(200, 200));  // this line does not do the job
        list.setMinimumSize (new Dimension (200,200));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(5);
        JScrollPane scrollPane_1 = new JScrollPane(list);
        panel.add(scrollPane_1 , BorderLayout.CENTER);

但这不会强制大小 200 200 。我需要 JScrollPane 的宽度为200

But this does not force the size to be 200 200. I need the width of the JScrollPane to be 200

推荐答案

不,不要设置已经添加到 JScrollpane JList 的大小。因为 JList 有一个实现的函数 getPreferredScrollableViewportSize(),它计算首选 viewport 通过计算它包含的单元格宽度和单元格高度来确定大小让 JScrollPane 来处理它。如果需要,您可以尝试添加 JScrollPane的维度。但是,要控制项目显示编号和方向,您可以使用:

No, don't set the size of the JList which is already added to JScrollpane. Because JList has an implemented function getPreferredScrollableViewportSize() which computes preferable viewport size by computing cell width and cell height it contains. Let the JScrollPane to handle that. If require, you try adding JScrollPane's dimension. However, to control the item display number and orientation, you can use:


  • setLayoutOrientation ,它允许列表在多列中显示其数据。值:

  • setLayoutOrientation which lets the list display its data in multiple columns. The value:


  1. JList.HORIZONTAL_WRAP :指定在包装到新行之前,列表应该从左到右显示其项目。

  2. JList.VERTICAL_WRAP :指定在包装到新列之前从上到下显示数据(照例)。

  1. JList.HORIZONTAL_WRAP: specifies that the list should display its items from left to right before wrapping to a new row.
  2. JList.VERTICAL_WRAP: which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column.


  • setVisibleRowCount(-1) 使列表显示屏幕上可用空间中可能的最大项目数。 setVisibleRowCount 的另一个常见用法是在列表的滚动窗格中指定列表优先显示的行数。

  • setVisibleRowCount(-1) makes the list display the maximum number of items possible in the available space onscreen. Another common use of setVisibleRowCount is to specify to the lists's scroll pane how many rows the list prefers to display.

    查看: 如何使用列表进行演示和示例。

    Check out: How to Use List for Demo and examples.

    这篇关于如何指定Jlist宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆