gpt4 book ai didi

java - 设置JList的最大容量?

转载 作者:行者123 更新时间:2023-11-30 11:15:33 24 4
gpt4 key购买 nike

我试图将我的 JList 的最大容量设置为 5 个元素。我似乎只能找到一种使用 .capacity() 设置最小值的方法。是否有将最大值设置为 5 并在超过此限制时显示对话框的方法?到目前为止我有这个:

String getProcessName = "";
getProcessName = jTextField1.getText();
if (info.size() >= 5)
{
JOptionPane.showMessageDialog(null,"5 elements reached");
}
else
{
info.addElement(getProcessName);
}

这会显示一条错误消息,但仍会将下一个值添加到此列表中。有什么建议吗?

最佳答案

JFrame frame = new JFrame();

//Other frame "stuff"

JList list = new JList(new Object[]{"121", "131", "141", "151" , "111", "181"});
list.setSelectionModel(new MySelectionModel(list, 5)); //<-- This is the magic line right here

frame.setVisible(true);

This accomplishes your task by setting the size of your custom selection model to 5.

关于java - 设置JList的最大容量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com