标签不会出现在java gui中

时间:2014-07-12 11:37:57

标签: java label

我是java的新手,所以如果这个问题很简单,我很抱歉。我在使用图层方面遇到了问题。

public class LoginPage extends JFrame {

private JButton loginBtn = new JButton("Login");
private JLabel nameLbl = new JLabel("UserName: ");

public LoginPage(){

    this.setTitle("Licence Management Software");
    this.setSize(640 , 480);
    this.setLayout(null);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loginBtn.setBounds(10,20,100,40);


    this.add(loginBtn);
    this.add(nameLbl);

    }

public static void main(String[] args) {
    new LoginPage();
}
}

按钮运行没有问题,但标签没有出现。谢谢你的帮助。我正在使用eclipse。

2 个答案:

答案 0 :(得分:0)

有些观点。

  • 添加完所有组件后,最后调用this.setVisible(true);

  • 请勿使用null布局。有很多布局经理,根据你的需要使用任何一个。

    A Visual Guide to Layout Managers

  • 不要使用setSize()方法,只需将其留给布局管理器根据组件的首选尺寸设置大小和位置。调用JFrame#pack()以适合框架中的所有组件。

答案 1 :(得分:0)

这是代码尝试这个

public class LoginPage extends JFrame {

private JButton loginBtn = new JButton("Login");
private JLabel nameLbl = new JLabel("UserName: ");
private Container container;

public LoginPage(){

container=this.getContentPane();
this.setTitle("Licence Management Software");
this.setSize(640 , 480);
this.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginBtn.setBounds(10,20,100,40);


container.add(loginBtn);
container.add(nameLbl);
this.setVisible(true);

}

public static void main(String[] args) {
new LoginPage();
}
}

希望这会对你有所帮助。

相关问题