gpt4 book ai didi

java - JComboBox 对于 JFrame 来说太长

转载 作者:行者123 更新时间:2023-12-01 21:09:13 24 4
gpt4 key购买 nike

我对 JComboBox 和长字符串有疑问。结果是 JComboBox 对于 JFrame 来说太长,因此它不会显示整个框。这里我有一些代码显示了我的问题:

    JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel = new JPanel();
DesignGridLayout layout = new DesignGridLayout(panel);
Vector<ArrayList<String>> content = new Vector<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
list.add("12345");
list.add("abcde");
content.add(list);
ComboBoxFullMenu<ArrayList<String>> combobox = new ComboBoxFullMenu<ArrayList<String>>(content);
combobox.setRenderer(new DefaultListCellRenderer() {

private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
ArrayList<String> array = (ArrayList<String>) value;
if (!array.isEmpty())
{
String text = "";
for (String info : array)
{
text += info + "; ";
}
setText(text);
}
return this;
}
});
AutoCompleteDecorator.decorate(combobox, new ObjectToStringConverter()
{

@Override
public String getPreferredStringForItem(Object object)
{
if (object != null)
{
if (object instanceof ArrayList)
{
ArrayList<String> list = (ArrayList<String>) object;
String text = "";
for (String info : list)
{
text += info + "; ";
}
return text;
}
else
{
return object.toString();
}
}
return null;
}
});
layout.row().grid().add(combobox);
frame.add(panel);
frame.setVisible(true);

这是 ComboBoxFullMenu 类:

public class ComboBoxFullMenu<E> extends JComboBox<E>
{

/**
*
*/
private static final long serialVersionUID = 1L;

public ComboBoxFullMenu(Vector<E> items)
{
super(items);
addActionListener(this);
}

public ComboBoxFullMenu()
{
super();
addActionListener(this);
}

public ComboBoxFullMenu (DefaultComboBoxModel<E> defaultComboBoxModel)
{
super(defaultComboBoxModel);
addActionListener(this);
}

/**
* Small hack to get pop up menu size bigger enough to show items even though
* the combo box size could be smaller
* */
private boolean layingOut = false;

@Override
public void doLayout()
{
try
{
layingOut = true;
super.doLayout();
}
finally
{
layingOut = false;
}
}

@Override
public Dimension getSize()
{
Dimension dim = super.getSize();
if ( !layingOut )
{
dim.width = Math.max(dim.width, getPreferredSize().width);
}
return dim;
}
}

我还尝试使用 DefaultListCellRenderer 和 ObjectTroStringConverter 以较短的方式显示所选项目,但我需要查看下拉菜单中的所有信息,并且我认为 JComboBox 计算其关于最长项目的宽度?我希望您理解我的问题,否则请告诉我。

最佳答案

查看Combo Box Popup允许您控制弹出窗口的宽度。可以设置宽度

  1. 最大字符串的大小,或
  2. 指定最大宽度。

如果字符串大于指定宽度,您可以在弹出窗口中打开滚动。

关于java - JComboBox 对于 JFrame 来说太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506839/

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