e775. 设置JList组件项的维数

By default, the width of the list is determined by the longest item and the height is determined by the number of visible lines multiplied by the tallest item. This example demonstrates how to override these values.

    // Create a list
    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);
    
    // Set the item width
    int cellWidth = 200;
    list.setFixedCellWidth(cellWidth);
    
    // Set the item height
    int cellHeight = 18;
    list.setFixedCellHeight(cellHeight);

It is also possible to set the item dimensions using a sample value:

    String protoCellValue = "My Sample Item Value";
    list.setPrototypeCellValue(protoCellValue);

 

Related Examples
posted @ 2018-09-06 08:48  borter  阅读(230)  评论(0编辑  收藏  举报