gpt4 book ai didi

java - JButton 占据整个框架

转载 作者:行者123 更新时间:2023-12-01 18:13:49 27 4
gpt4 key购买 nike

我正在创建一个登录系统,当我制作登录按钮时,它占据了整个框架。我尝试了 .setBounds(); 但它不起作用。不知道我做错了什么,请帮忙。

    import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main extends JFrame{
public Main(){
JTextField name = new JTextField("Username");
JTextField pass = new JTextField("Password");
JButton login = new JButton("Login");
name.setBounds(230, 110, 100, 25);
pass.setBounds(230, 145, 100, 25);
login.setBounds(230, 165, 100, 25);
add(name);
add(pass);
add(login);
}

public static void main(String [] args) {
Main a = new Main();
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setSize(500, 300);
a.setLocationRelativeTo(null);
a.setVisible(true);
a.setTitle("Login System");
a.setResizable(false);
a.setLayout(new FlowLayout());

}

}

最佳答案

在构造函数中,尝试 setLayout(new FlowLayout());

这可能不会导致您想要的结果,但从那里您可以进一步调查。我建议您在这里阅读有关布局的信息: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

关于java - JButton 占据整个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com