0

嗨,我是 java swing 的新手,我尝试了很多并进行了很多搜索,但徒劳无功。我想在我已经拥有的自动化 jFrame 上显示一个 jlabel(不是通过拖放,而是通过代码)。好吧,我的我的 JLabel 的代码如下: private JLabel la=new JLabel("Display label");

我在搜索显示在 jpanel 中的代码如下: jPanel1.add(jLabel1); 但它不起作用。我的 jPanel1 是通过拖放构建的,名称为 Jpanel1。

我什至尝试了这段代码: this.add(jLabel1)-- this.getContentPane().add(jLabel1) 但仍然无法正常工作。

请帮助我,它必须是一行简单的代码,否则我在某处遗漏了一些东西,拜托..谢谢

4

1 回答 1

0

This is a simple piece of code obtained from link text

public class HelloWorldFrame extends JFrame {

    public static void main(String args[]) {
        new HelloWorldFrame();
    }
    HelloWorldFrame() {
        JLabel jlbHelloWorld = new JLabel("Hello World");
        add(jlbHelloWorld);
        this.setSize(100, 100);
        // pack();
        setVisible(true);
    }
}

What you need to do is to re-set the setVisible property of the parent component to true

于 2011-01-16T21:12:13.327 回答