2

I am creating a Java application which has a login screen. Right now I am designing the login screen. I use a Panel in a JFrame, which is positioned at BorderLayout.CENTER, and then set the layout of the Panel as GridLayout(3,1). Right now my application looks like this:

Rightnow how my image looks

I want to design my JFrame like this:

My design idea

Here the grey portion is the JFrame and the red portion is the Panel which has the login screen, TextField's and Button's. I don't want the TextField and Button's on the entire screen.

This is what I did right now

public class MailClient {

JFrame loginScreen;
JTextField emailid;
JPasswordField password;
JButton login;
Dimension loginScreenSize;
JPanel entireLogin;
BorderLayout mainLayout;

public static void main (String args[])
{
    MailClient obj = new MailClient();
    obj.build();
}
public void build()
{
    mainLayout = new BorderLayout(40, 40);

    loginScreenSize = new Dimension(300,300);

    loginScreen = new JFrame("MailClient");
    loginScreen.setSize(loginScreenSize);
    loginScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loginScreen.setResizable(false);
    loginScreen.setLocationRelativeTo(null);
    loginScreen.setLayout(mainLayout);

    entireLogin = new JPanel();
    entireLogin.setLayout(new GridLayout(3,1));

    emailid = new JTextField();
    password = new JPasswordField();
    login = new JButton("LOGIN");

    entireLogin.add(emailid);
    entireLogin.add(password);
    entireLogin.add(login);

    loginScreen.add(entireLogin, BorderLayout.CENTER);
    loginScreen.setVisible(true);
}

}
msrd0
  • 7,816
  • 9
  • 47
  • 82
capt.swag
  • 10,335
  • 2
  • 41
  • 41

1 Answers1

1

Add an empty border of the thickness you find appropriate around the centered JPanel if you want it to have a border to the edge (if it resizes, for example).

myPanel.setBorder(new EmptyBorder(int, int, int, int))

Set the maximum size for the centered JPanel if you want to prevent it from being made larger by the BorderLayout CENTER normal workings.

myPanel.setMaximumSize(new Dimension(x, y))
Xabster
  • 3,710
  • 15
  • 21
  • 1
    *"Set the maximum size for the centered JPanel.."* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Aug 08 '14 at 15:24