Java JCombobox导致渲染问题
您好,我遇到以下问题:
public class TestCombo extends JFrame{
public TestCombo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,4));
JLabel l1 = new JLabel("test1");
JLabel l2 = new JLabel("test2");
panel.add(l1);
panel.add(l2);
// JComboBox<String> combo = new JComboBox<String>();// <-- uncomment this for the problem
this.add(panel, BorderLayout.NORTH);
}
public static void main(String[] args) {
new TestCombo();
}
}
如您所见,我正在做一个非常简单的示例。如果我取消注释标记的部分,则不会显示标签项。如果我调整窗口大小,它们会再次可见。这里奇怪的是,我什至没有将组合添加到面板或任何地方。我只是实例化它。 有人可以告诉我为什么需要调整框架大小才能看到标签吗?我做错了什么吗?
Hello, I have the following problem:
public class TestCombo extends JFrame{
public TestCombo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,4));
JLabel l1 = new JLabel("test1");
JLabel l2 = new JLabel("test2");
panel.add(l1);
panel.add(l2);
// JComboBox<String> combo = new JComboBox<String>();// <-- uncomment this for the problem
this.add(panel, BorderLayout.NORTH);
}
public static void main(String[] args) {
new TestCombo();
}
}
As you can see, I am doing a very simple example. If I uncomment the marked part, the label items are not shown. If I resize the window, they are visible again. The strange thing here is that I do not even add the combo
to the panel or anywhere. I am just instantiating it.
Can someone tell me why I need to resize the frame to see the labels? Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从一开始就在执行
setVisible(true)
。您应该在添加所有组件后执行此操作。
you are doing
setVisible(true)
in the very beginning.You should do it after adding all components.
我认为这不是 JComboBox 的有效语法,
它应该是
setVisible(true);
应该在this.add(panel, BorderLayout.NORTH);
之后。如何使用组合框
I don't think this is valid syntax for JComboBox
it should be
Also
setVisible(true);
should be afterthis.add(panel, BorderLayout.NORTH);
.How to use comboBox