Jlabel在单独的类中实现时不会出现在Jframe中

发布于 2025-02-05 17:12:51 字数 812 浏览 3 评论 0原文

我正在尝试将Jlabel添加到我的Jframe中。我已经在单独的类中实现了它们,但是当我运行代码时,我看不到我的标签。

当我在app类中实现帧和标签时,它运行良好。

import javax.swing.JFrame;

public class MyFrame extends JFrame {

    public MyFrame() {
        this.setSize(420, 420); 
        this.setTitle("First Java GUI");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }    
}
import javax.swing.JLabel;

public class MyLabel extends JLabel {
    public MyLabel() {
        JLabel label = new JLabel();
        label.setText("Welcome");
    }
}
public class App {
    public static void main(String[] args) {
        
        MyFrame frame1 = new MyFrame();
        MyLabel label1 = new MyLabel();

        frame1.add(label1);
    }
}

I am trying to add a JLabel to my JFrame. I have implemented them in separate classes but when I run the code, I cannot see my label.

It worked well when I implemented both frame and label in the App class.

import javax.swing.JFrame;

public class MyFrame extends JFrame {

    public MyFrame() {
        this.setSize(420, 420); 
        this.setTitle("First Java GUI");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }    
}
import javax.swing.JLabel;

public class MyLabel extends JLabel {
    public MyLabel() {
        JLabel label = new JLabel();
        label.setText("Welcome");
    }
}
public class App {
    public static void main(String[] args) {
        
        MyFrame frame1 = new MyFrame();
        MyLabel label1 = new MyLabel();

        frame1.add(label1);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

莫多说 2025-02-12 17:12:54

您在mylabel类中遇到了错误。如果扩展它,只需添加构造函数即可。但是您在构造函数中创建了另一个标签,这不会添加到框架中。因此,正确的版本是:

import javax.swing.JLabel;

public class MyLabel extends JLabel {
    
    public MyLabel(String text) {
        super(text);
    }
    
    public MyLabel() {
    }
}

之后:

public class App {
    public static void main(String[] args) {
        
        MyFrame frame1 = new MyFrame();
        MyLabel label1 = new MyLabel("Welcome");

        frame1.add(label1);
    }
}

You have a mistake in MyLabel class. If you extend it, just add constructors. But you in your constructor create another label, that is not added to frame. So, correct version is:

import javax.swing.JLabel;

public class MyLabel extends JLabel {
    
    public MyLabel(String text) {
        super(text);
    }
    
    public MyLabel() {
    }
}

And after it:

public class App {
    public static void main(String[] args) {
        
        MyFrame frame1 = new MyFrame();
        MyLabel label1 = new MyLabel("Welcome");

        frame1.add(label1);
    }
}
茶底世界 2025-02-12 17:12:54
import javax.swing.JLabel;

public class MyLabel extends JLabel {
    public MyLabel() {
        super();
        setText("Welcome");
    }
}

是您需要的。您将代码隐藏在另一个jlabel中,它不是不是您的子类。不过,这是一个问题:您为什么要创建一个子类?为什么不立即使用jlabel

import javax.swing.JLabel;

public class MyLabel extends JLabel {
    public MyLabel() {
        super();
        setText("Welcome");
    }
}

is what you need. You hide your code inside another JLabel which is not your subclass. It sort of begs the question though: why are you creating a subclass? Why not just use a JLabel right off?

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