如何设置JComboBox下拉框高度
我用netBeans做的界面,JComboBox下拉框的内容是后添加进去的,用.addItem。可是下拉框的高度却是1行,只能看到一条选项。焦点移开后再点击就正常了。问题...
我用netBeans做的界面,JComboBox下拉框的内容是后添加进去的,用.addItem。可是下拉框的高度却是1行,只能看到一条选项。焦点移开后再点击就正常了。
问题:有没有办法指定这个下拉框的高度或者让它自动调整为最适合的? 展开
问题:有没有办法指定这个下拉框的高度或者让它自动调整为最适合的? 展开
展开全部
给你个例子,对照一下看看。
再不行把源码发来看看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTest {
public static void main(String[] args) {
ComboBoxFrame frame = new ComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ComboBoxFrame extends JFrame {
private JComboBox faceCombo;
private JLabel label;
private static final int DEFAULT_SIZE = 12;
public ComboBoxFrame() {
setTitle("ComboBoxTest");
setSize(300,200);
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
faceCombo = new JComboBox();
faceCombo.setEditable(true);
faceCombo.addItem("Serif");
faceCombo.addItem("SansSerif");
faceCombo.addItem("Monospaced");
faceCombo.addItem("Dialog");
faceCombo.addItem("DialogInput");
faceCombo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
label.setFont(new Font((String)faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE));
}
});
JPanel comboPanel = new JPanel();
comboPanel.add(faceCombo);
add(comboPanel, BorderLayout.SOUTH);
}
}
再不行把源码发来看看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTest {
public static void main(String[] args) {
ComboBoxFrame frame = new ComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ComboBoxFrame extends JFrame {
private JComboBox faceCombo;
private JLabel label;
private static final int DEFAULT_SIZE = 12;
public ComboBoxFrame() {
setTitle("ComboBoxTest");
setSize(300,200);
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
faceCombo = new JComboBox();
faceCombo.setEditable(true);
faceCombo.addItem("Serif");
faceCombo.addItem("SansSerif");
faceCombo.addItem("Monospaced");
faceCombo.addItem("Dialog");
faceCombo.addItem("DialogInput");
faceCombo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
label.setFont(new Font((String)faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE));
}
});
JPanel comboPanel = new JPanel();
comboPanel.add(faceCombo);
add(comboPanel, BorderLayout.SOUTH);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询