我试图理解java中的GUI。我开始制作一个简单的登录页面。在这里,我将标题面板、正文面板和页脚面板堆叠在一个盒子布局中(垂直堆叠)。所有这些都在另一个父面板中,然后将其放入框架的内容窗格(中心,边框布局)。但是,在父面板内部的主体面板都被垂直拉伸了。修复它非常困难,有什么建议吗?
我不想在这里使用弹簧布局等。
setSize、setMinimumSize、setMaximumSize 似乎根本不在这里工作。
请帮忙
JButton submit = new JButton("Login"); //Directly added to the frame
JLabel head1 = new JLabel("LOGIN SCREEN"); //Component of head panel
JLabel userName = new JLabel("Username "); //Component of body panel 1
JTextField userNameField = new JTextField(15); //Component of body panel 1
JLabel userNameExtra = new JLabel(" *"); //Component of body panel 1
userNameExtra.setForeground(Color.red);
JLabel pass = new JLabel("Password "); //Component of body panel 2
JPasswordField passField = new JPasswordField(15); //Component of body panel 2
JLabel passExtra = new JLabel(" *"); //Component of body panel 2
passExtra.setForeground(Color.red);
JLabel footer = new JLabel ("*This field is mandatory");
JLabel credits = new JLabel("-by Nikhil");
//---Preparing the panels with their components---
headPanel.setLayout(new BoxLayout(headPanel,BoxLayout.Y_AXIS));
headPanel.add(head1);
//userNamePanel.setLayout(new BoxLayout(userNamePanel,BoxLayout.X_AXIS));
userNamePanel.add(userName);
userName.requestFocus();
userNamePanel.add(userNameField);
userNamePanel.add(userNameExtra);
//passPanel.setLayout(new BoxLayout(passPanel,BoxLayout.X_AXIS));
passPanel.add(pass);
passPanel.add(passField);
passPanel.add(passExtra);
bodyPanel.setLayout(new BoxLayout(bodyPanel,BoxLayout.Y_AXIS));
bodyPanel.add(userNamePanel);
bodyPanel.add(passPanel);
bodyPanel.add(submit);
footerPanel.setLayout(new BoxLayout(footerPanel,BoxLayout.Y_AXIS));
footerPanel.add(footer);
//---Preparing the parent panel with its components (other panels)---
parentPanel.setLayout(new BoxLayout(parentPanel,BoxLayout.Y_AXIS));
parentPanel.add(headPanel);
parentPanel.add(bodyPanel);
parentPanel.add(footerPanel);
//---Putting the parent panel in the frame---
frame.setTitle("Login GUI Application");
frame.getContentPane().add(parentPanel,"Center");
frame.getContentPane().add(credits,"South");
frame.setSize(500,500);
frame.setVisible(true);
}
}