繁体   English   中英

在其他类中创建 JLabel 并实现到主类未出现

[英]created JLabel in other class and implemented to main class not appears

我想让我的代码更清晰,并将标签从登录屏幕移动到其他类。 当我在主类中实现 JLabel 时它工作正常,但是当它移到外面时它没有出现。

public LoginPanel ()  {

    frame = new JFrame ("Login");                               // initial frame, add title
    frame.setSize(500, 500);                                    // frame size
    frame.setLocation(300, 200);                                // set where program window should start
    frame.setLayout(null);                                      // set layout; you can use (new FlowLayout (FlowLayout.LEFT));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       // closing the program by clicking X
    /*
    userLabel = new JLabel("enter user name label");            // create Label next to the user field
    userLabel.setLocation(10, 10);                              // set location where label will start to appear
    userLabel.setSize (userLabel.getPreferredSize());           //
    frame.add(userLabel);                                       // add userLabel to the frame
    */
    CredentialsFields fields = new CredentialsFields();
    frame.add(fields);

新班级到我想要移动 JLABEL 的地方

public class CredentialsFields extends JComponent{

    JLabel userLabel;   

    public CredentialsFields() {

        super();
        userLabel = new JLabel("enter user name label");            // create Label next to the user field
        this.setSize (500,300); 
        this.setLocation(10, 10);       

尝试这个


public class CredentialsFields extends JPanel {

    JLabel userLabel;   

    public CredentialsFields() {

        super();
        userLabel = new JLabel("enter user name label");            // create Label next to the user field
        this.setSize (500,300); 
        this.setLocation(10, 10); 
        add(userLabel); // add the label to the current panel
}

  1. 对于复合组件,最好从JPanel继承
  2. 您没有将标签添加到容器CredentialsFields

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM