我试图在我的 JFrame 上放置一个 JComboBox,如果我单击显示组合的内容,但它没有显示在 JFrame 上。
我已经尝试过:.setVisible(true)、.setEnabled(true) 等。
这是我的代码:
public class tryCode {
private final JComboBox vehicleTypeBox = new JComboBox(new String[] {"HELLO WORLD", "OLA K ASE"});
private JFrame frame;
public tryCode() {
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(null);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
showComboBox();
}
public void showComboBox() {
vehicleTypeBox.setBounds(10,80,100,30);
vehicleTypeBox.setSelectedIndex(0);
frame.add(vehicleTypeBox);
}
}
欢迎任何解决方案!谢谢
最佳答案
您永远不会在任何地方调用 showComboBox()
。如果它应该在启动时显示,您应该在构造函数中调用它。
public tryCode()
{
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(new FlowLayout()); // do not use null!
frame.setResizable(false);
frame.setLocationRelativeTo(null);
showComboBox();
frame.setVisible(true);
}
关于java - JComboBox 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16454685/