Java 如何指定Jlist宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19819723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify Jlist width?
提问by Donotello
I want to specify fixed widthof my JList
我想指定我的 JList 的固定宽度
I followed example provided by Gilbert Le Blanc 22k
我遵循了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);
But this does not force the size to be 200 200
. I need the width of the JScrollPane
to be 200
但这并不强制大小为200 200
. 我需要的宽度为JScrollPane
200
采纳答案by Sage
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:
不,不要设置JList
已经添加到JScrollpane
. 因为JList
有一个实现的函数getPreferredScrollableViewportSize()
,它viewport
通过计算它包含的单元格宽度和单元格高度来计算优选大小。让JScrollPane
来处理。如果需要,您可以尝试添加JScrollPane's
维度。但是,要控制项目显示数量和方向,您可以使用:
setLayoutOrientation
which lets the list display its data in multiple columns. The value:JList.HORIZONTAL_WRAP
: specifies that the list should display its items from left to right before wrapping to a new row.JList.VERTICAL_WRAP
: which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column.
setVisibleRowCount(-1)
makes the list display the maximum number of items possible in the available space onscreen. Another common use ofsetVisibleRowCount
is to specify to the lists's scroll pane how many rows the list prefers to display.
setLayoutOrientation
这让列表在多列中显示其数据。价值:JList.HORIZONTAL_WRAP
: 指定列表在换行到新行之前应从左到右显示其项目。JList.VERTICAL_WRAP
: 指定在换行到新列之前从上到下显示数据(像往常一样)。
setVisibleRowCount(-1)
使列表在屏幕上的可用空间中显示尽可能多的项目。的另一个常见用途setVisibleRowCount
是为列表的滚动窗格指定列表希望显示的行数。
Check out: How to Use Listfor Demo and examples.
查看:如何使用列表进行演示和示例。
回答by 3yakuya
Normally your list's size is the number of elements you put into it, so it won't get fixed this way. You can't create a list of N empty spaces, because it would not be a list then (what would be its length?) - an array would suit here much better. If you want to create a fixed-size list refer to:
通常,您的列表大小是您放入其中的元素数量,因此它不会以这种方式固定。你不能创建一个包含 N 个空格的列表,因为它不会是一个列表(它的长度是多少?) - 一个数组更适合这里。如果要创建固定大小的列表,请参阅:
You may also create a class which allows you to add a specified number of elements to your list and then forbids further addition (when your list's size gets to desired N).
您还可以创建一个类,它允许您向列表中添加指定数量的元素,然后禁止进一步添加(当您的列表大小达到所需的 N 时)。
回答by Coneone
The solution is to resize the JScrollPane
. If I remember correctly, JList
fill up the JScrollPane
.
解决方案是调整JScrollPane
. 如果我没记错的话,JList
填写JScrollPane
.
JScrollPane scrollPane_1 = new JScrollPane(list);
scrollPane_1.setMaximumSize(new Dimension(200, 200));
scrollPane_1.setMinimumSize (new Dimension (200,200));
panel.add(scrollPane_1 , BorderLayout.CENTER);
回答by camickr
it is about the width of the JScrollPane
它是关于 JScrollPane 的宽度
Most layout managers use the preferred size to determine the optimal size and then use min/max to handle resizing if the frame is ever resized.
大多数布局管理器使用首选大小来确定最佳大小,然后在框架被调整大小时使用 min/max 来处理调整大小。
I would try something like:
我会尝试类似的东西:
JList list = new JList(queries);
list.setVisibleRowCount(5);
JScrollPane scrollPane_1 = new JScrollPane(list);
Dimension d = list.getPreferredSize();
d.width = 200;
scrollPane_1.setPreferredSize(d);
Although it is rarely a good idea to manually set a preferred size of a component. Let the LAF determine the appropriate preferred size. For example I would never hard code height, because then your list might display 4 1/2 rows which is unprofessional looking.
尽管手动设置组件的首选大小很少是一个好主意。让 LAF 确定合适的首选大小。例如,我永远不会硬编码高度,因为那样您的列表可能会显示 4 1/2 行,这看起来很不专业。
The width I can understand a little bit if you only ever have a single character string it may look too narrow. But in this case a better solution would be to create a custom renderer and use an EmptyBorder that allocates extra space on the left/right and then let the LAF size the JList.
如果您只有一个字符串,我可以稍微理解一下宽度,它可能看起来太窄了。但在这种情况下,更好的解决方案是创建自定义渲染器并使用 EmptyBorder 在左侧/右侧分配额外空间,然后让 LAF 调整 JList 的大小。
回答by Pooya
You can simply use setFixedCellWidth
of your JList.
您可以简单地使用setFixedCellWidth
您的 JList。
回答by Chris Adams
// Loop through the arrayList of doubles and convert to String
// with dollar signs, 2 decimal places, and extra spacing.
for(int i = 0; i < bookPrices.size(); i++)
{
bookPricesOnlyStrings.add(String.format("$%,.2f%-35s", bookPrices.get(i), ""));
System.out.println(bookPricesOnlyStrings.get(i));
}
I had the same problem. My data was only 5 characters and looked off compared to my other window. So I used String.format to add spaces to the end of it. This widened my JList. I also was able to add dollar signs and format the decimal places.
我有同样的问题。与我的另一个窗口相比,我的数据只有 5 个字符,并且看起来不太好。所以我使用 String.format 在它的末尾添加空格。这扩大了我的 JList。我还能够添加美元符号并格式化小数位。