centering/sizing fields on a panel
I am having a hard time getting my text field and password fields to size correctly and to appear in the middle of the screen. Any help?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginScreen extends JFrame
{
public static void main(String[] args) {
new LoginScreen();
}
public LoginScreen() {
super("Log-In");
this.setDefaultCloseOperation(super.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setPreferredSize(new Dimension(400,200));
content.setBackground(Color.black);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(Box.createRigidArea(new Dimension(0,100)));
JFormattedTextField user = new JFormattedTextField();
JPasswordField pword = new JPasswordField();
content.add(user, BorderLayout.PAGE_START);
user.setSize(5,2);
content.add(Box.createRigidArea(new Dimension(0,30)));
content.add(pword, BorderLayout.PAGE_START);
pword.setSize(5,2);
pack();
setVisible(true);
}
}
Re: centering/sizing fields on a panel
Hi,
first I would put this after setDefaultCloseOperation():
Code:
this.setLocationRelativeTo(null);
so your JFrame will be placed in the middle of you screen.
And for the textfield itself I would use the GridLayout(int rows,int columns);
In that way you don't have to place the size. All component will have the same size and would be allign.