center is not public in BorderLayout ;cannot be accessed from outside the package

92 views Asked by At

I'm writing code to create a basic login screen in JFrame. I've created the JFrame and all required components but when I try to use BorderLayout I get this error message "center is not public in BorderLayout ;cannot be accessed from outside the package"

I have tried searching on different sites but have not got any solution. Is there any member getter function I need to use to access them? or am I making any mistake in my code?

import javax.swing.*;
import java.awt.*;

public class myMain
{
public static void main (String args [])
{
    JFrame login = new JFrame("Login window");
    JTextField user = new JTextField("Username");
    JPasswordField password = new JPasswordField("Password");
    JButton loginButton = new JButton("Login");


    login.setLayout(new BorderLayout());
    login.add(user, BorderLayout.north);
    login.add(password, BorderLayout.center);
    login.add(loginButton, BorderLayout.south);

    login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    login.setSize(800,600);
    login.setVisible(true);

}

Thanks for the help in advance.

1

There are 1 answers

0
Varun Kumar On

NORTH, CENTER and SOUTH (Notice the Uppercase)

import javax.swing.*;
import java.awt.*;

public class MyMain {
    public static void main (String args []) {
        JFrame login = new JFrame("Login window");
        JTextField user = new JTextField("Username");
        JPasswordField password = new JPasswordField("Password");
        JButton loginButton = new JButton("Login");


        login.setLayout(new BorderLayout());
        login.add(user, BorderLayout.NORTH);
        login.add(password, BorderLayout.CENTER);
        login.add(loginButton, BorderLayout.SOUTH);

        login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        login.setSize(800,600);
        login.setVisible(true);

    }
}