如何修复一个 JButton 以免占用整个 JFrame



我是编码初学者,我正在学习Java。我正忙着做一个登录系统,我已经做了一个JFrame,但是当我添加一个JButton时,它占用了整个JFrame

public class LogInSystem extends Application{
    @Override
    public void start(Stage primaryStage)
    {
        // Setting the JFrame
        JFrame frame = new JFrame("Log in System");
        frame.setSize(2000, 2000);
        frame.setVisible(true);
        // Setting the button
        JPanel panel = new JPanel();
        Button btn1 = new Button();
        btn1.setText("1");
        btn1.setBounds(50, 150, 100, 30);
        frame.add(btn1);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

似乎您声明了一个面板,但没有在其中添加按钮,也没有将面板添加到框架中。您可以尝试将按钮添加到面板中,将面板添加到框架中,

panel.add(btn1);
frame.add(panel);

您还可以为特定面板使用一些有用的布局。例如,BoxLayout,GridLayout等。默认情况下,所有内容都设置为流布局。

最新更新