Java JCombobox导致渲染问题

发布于 2024-12-18 15:49:23 字数 852 浏览 0 评论 0原文

您好,我遇到以下问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-12-25 15:49:23

您从一开始就在执行 setVisible(true)

您应该在添加所有组件后执行此操作。

you are doing setVisible(true) in the very beginning.

You should do it after adding all components.

安稳善良 2024-12-25 15:49:23

我认为这不是 JComboBox 的有效语法,

JComboBox<String> combo = new JComboBox<String>();

它应该是

JComboBox combo = new JComboBox();

setVisible(true); 应该在 this.add(panel, BorderLayout.NORTH); 之后。


如何使用组合框

I don't think this is valid syntax for JComboBox

JComboBox<String> combo = new JComboBox<String>();

it should be

JComboBox combo = new JComboBox();

Also setVisible(true); should be after this.add(panel, BorderLayout.NORTH);.


How to use comboBox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文