gpt4 book ai didi

java - 调整 jcombobox 下拉菜单的宽度

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:05 25 4
gpt4 key购买 nike

有没有办法调整 JCombobox 的下拉窗口大小?

假设我有:

comArmor.setBounds(81, 102, 194, 26);

但是当用户选择该框并弹出下拉列表时,我希望下拉窗口展开以便完整显示一长行文本(比如 size x of 300)。

这可能吗?

最佳答案

enter image description here

即使组合框尺寸可以更小,也可以使弹出菜单尺寸足够大以显示项目的小技巧

来源:http://www.jroller.com/santhosh/entry/make_jcombobox_popup_wide_enough

    import java.awt.Dimension;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

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

public ComboBoxFullMenu(E[] items) {
super(items);
addActionListener(this);
}

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

public ComboBoxFullMenu(ComboBoxModel<E> aModel) {
super(aModel);
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;
}
}

关于java - 调整 jcombobox 下拉菜单的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057457/

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