【问题标题】:java gui setbounds doesnt workjava gui setbounds 不起作用
【发布时间】:2014-09-25 08:02:47
【问题描述】:

我正在尝试用 java 制作一个 UI,如果这是一个简单的问题,我很抱歉。

public class viewDeneme extends JFrame {

private static final long serialVersionUID = -7284396337557548747L;
private JTextField nameTxt = new JTextField(10);
private JTextField passwordTxt = new JTextField(10);
private JButton loginBtn = new JButton("Giriş");
private JLabel nameLbl = new JLabel("Kullanıcı adi:");
private JLabel passwordLbl = new JLabel("Şifre:");

public viewDeneme(){
    JPanel loginPanel = new JPanel();
    this.setSize(600,200);
    this.setLocation(600, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    nameLbl.setBounds(200, 200, 100, 50);

    loginPanel.add(nameTxt);
    loginPanel.add(passwordTxt);
    loginPanel.add(loginBtn);
    loginPanel.add(nameLbl);
    loginPanel.add(passwordLbl);

    this.setVisible(true);
    this.add(loginPanel);
}

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

}
}

这是我的代码。我正在尝试为标签和文本框设置边界,但它没有改变任何东西。没有任何错误,所以我必须遗漏一些东西,但我在网络上搜索时找不到它。谢谢你帮助

【问题讨论】:

    标签: java user-interface setbounds


    【解决方案1】:

    What is setBounds and how do I use it?

    JPanel 布局必须为 null 才能使用绝对定位。 JPanel 对象被初始化为使用 FlowLayout,除非您在创建 JPanel 时指定不同。所以你必须写

    loginPanel.setLayout(null);
    

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多