如何让combobox的列表更宽广?

import javax.swing.*; public class test { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(120,80); JComboBox cb = new JComboBox(); cb.addItem("A very long combo-box item that doesn't fit no. 1"); cb.addItem("A very long combo-box item that doesn't fit no. 2"); frame.add(cb); frame.validate(); frame.setVisible(true); } } 

如何使combobox项目以其所有文本可见的方式显示?
现在我有这样的事情:
在此处输入图像描述
我不想在折叠时更改combobox的大小。
我只是想增加扩展部分的宽度。

我认为这可能有所帮助。

链接代码

 public class WiderDropDownCombo extends JComboBox { private String type; private boolean layingOut = false; private int widestLengh = 0; private boolean wide = false; public WiderDropDownCombo(Object[] objs) { super(objs); } public boolean isWide() { return wide; } // Setting the JComboBox wide public void setWide(boolean wide) { this.wide = wide; widestLengh = getWidestItemWidth(); } public Dimension getSize() { Dimension dim = super.getSize(); if (!layingOut && isWide()) dim.width = Math.max(widestLengh, dim.width); return dim; } public int getWidestItemWidth() { int numOfItems = this.getItemCount(); Font font = this.getFont(); FontMetrics metrics = this.getFontMetrics(font); int widest = 0; for (int i = 0; i < numOfItems; i++) { Object item = this.getItemAt(i); int lineWidth = metrics.stringWidth(item.toString()); widest = Math.max(widest, lineWidth); } return widest + 5; } public void doLayout() { try { layingOut = true; super.doLayout(); } finally { layingOut = false; } } public String getType() { return type; } public void setType(String t) { type = t; } public static void main(String[] args) { String title = "Combo Test"; JFrame frame = new JFrame(title); String[] items = { "I need lot of width to be visible , oh am I visible now", "I need lot of width to be visible , oh am I visible now" }; WiderDropDownCombo simpleCombo = new WiderDropDownCombo(items); simpleCombo.setPreferredSize(new Dimension(180, 20)); simpleCombo.setWide(true); JLabel label = new JLabel("Wider Drop Down Demo"); frame.getContentPane().add(simpleCombo, BorderLayout.NORTH); frame.getContentPane().add(label, BorderLayout.SOUTH); int width = 200; int height = 150; frame.setSize(width, height); frame.setVisible(true); } } 

更多有点复杂的解决方法是@camickr实现的Combo Box Popup

  • setScrollBarRequired – 如果为true,则在必要时将自动显示水平滚动条
  • setPopupWider – 如果为true,则弹出窗口的宽度将基于combobox中的项目。 宽度永远不会小于combobox。
  • setMaximumWidth – 可以控制弹出窗口的宽度,以防你有一个不合理的长项目来渲染。
  • setPopupAbove – 当为true时,弹出窗口将显示在combobox上方。
  • 通过PopupMenuListener监听

缩减版本的Pshemo给出的代码:

 import javax.swing.JComboBox; import java.awt.Dimension; public class JComboBoxWider extends JComboBox { private int listWidth=0; public JComboBoxWider(Object[] objs) { super(objs); } public Dimension getSize() { Dimension dim = super.getSize(); if (listWidth>0) dim.width = listWidth; return dim; } public void setListWidth(int listWidth) { this.listWidth = listWidth; } } 

Warrio的回答对我有用。 我添加了一点来制作通用版本。

 import java.awt.Dimension; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; public class JComboBoxWider extends JComboBox { private static final long serialVersionUID = 1L; private int listWidth=0; public JComboBoxWider(ComboBoxModel objs) { super(objs); } public Dimension getSize() { Dimension dim = super.getSize(); if (listWidth > 0) dim.width = listWidth; return dim; } public void setListWidth(int listWidth) { this.listWidth = listWidth; } } 

你没有使用Netbeans,拖放。 然后简单地使用:

 JComboBox cb = new JComboBox(); cb.seBounds(10,10,200,30); 

您正在创建一个JComboBox对象,但没有修复它的宽度和长度等等。在创建JComboBox的对象后,使用setBound()方法。 喜欢这个:

 JComboBox cb = new JComboBox(); cb.setBounds(10,10,200,30);