0

I have two components ready to add to frame:

class Lamina extends JPanel{

    public Lamina(){

        setLayout(new BorderLayout(50,50));

        JPasswordField user_password = new JPasswordField();
        add(user_password, BorderLayout.SOUTH);

    }

}

class DOMHeader extends JPanel
{
    public DOMHeader()
    {
        setLayout(new BorderLayout());

        JLabel title = new JLabel("Sign in");
        add(title, BorderLayout.NORTH);
    }
}

This is my class UI:

public class UI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setTitle("Metin2");

    }  
}

Frame class:

class Frame extends JFrame {

    public Frame() {
        Toolkit screen = Toolkit.getDefaultToolkit();
        Dimension screenSize = screen.getScreenSize();

        int widthScreen = screenSize.width;
        int heightScreen = screenSize.height;

        setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));

        Image icon = screen.getImage("icon.jpeg");

        setIconImage(icon);


        /* Add to Components to frame*/
        add(new DOMHeader());
        add(new Lamina());

    }

}

In my class Frame I'm adding the components shown earlier, but it "put on top of" a component another component.

According to the API:

public Component add(Component comp,int index)

Adds the specified component to this container at the given position (index).

I run the main method:

enter image description here

As you can see it only show the Component DOMHeader class: add(new DOMHeader())

And what happened with add(new Lamina())

What number or Constant I should give it ?

jjoselon
  • 2,641
  • 4
  • 23
  • 37
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Feb 07 '18 at 15:21
  • Using null layout will add every component at 0,0, you should go with a LayoutManager such AxisLayout, the index is only used to determine which component is draw first (the first one is on bottom of others) – Marcos Vasconcelos Feb 07 '18 at 15:36
  • @MarcosVasconcelos he's not using `null layout`. He just called `setBounds(...)` on the `JFrame` itself to position it on the screen – Frakcool Feb 07 '18 at 15:40

1 Answers1

3

This line:

class Frame extends JFrame {

is incorrect for the following reasons:

  1. It's confusing because of AWT Frame class
  2. It extends JFrame but you never change the behavior of the JFrame later, so no need to extend JFrame but create it inside of class

Now, we must head to JFrame class in which says:

The default content pane will have a BorderLayout manager set on it.

Now, if we go to the visual guide for layout managers in the BorderLayout section we can see the following image:

enter image description here

Which shows us that we can only add our components to 5 locations:

  • PAGE_START (or NORTH)
  • LINE_START (or WEST)
  • CENTER
  • LINE_END (or EAST)
  • PAGE_END (or SOUTH)

To answer your question:

According to the API:

public Component add(Component comp,int index)

Adds the specified component to this container at the given position (index).

What number or Constant I should give it ?

Well you need to add it as follows:

JFrame frame = new JFrame("Write your title here"); //Follow first advice
frame.add(new DOMHeader(), BorderLayout.NORTH); //These are the constants.
frame.add(new Lamina(), BorderLayout.SOUTH);

You confused yourself by adding the items in their locations in their own JPanels not on the JFrame itself.

Side note: Why are you doing these weird calculations?

setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));

Wouldn't it be clearer if you called: widthScreen / 8?


As per @camickr comment:

There is no need for the DOMHeader and Lamina classes. You don't need to extend JPanel just to add a component to the frame. The "content pane" of a JFrame is a JPanel, so you can just add the label and text field to the frame as shown above.

You can also have your code improved in this way:

JFrame frame = new JFrame("Your title here");
JLabel title = new JLabel("Sign in");
JPasswordField userPassword = new JPasswordField(); //changed variable name to userPassword instead of user_password to follow Java naming conventions

frame.add(title, BorderLayout.NORTH);
frame.add(userPassword, BorderLayout.SOUTH);

frame.setVisible(true); //This line should be the last one or you'll find yourself with strange "bugs" when your application starts until you move it or resize it

No need to create a whole new JPanel for each component.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    There is no need for the DOMHeader and Lamina classes. You don't need to extend JPanel just to add a component to the frame. The "content pane" of a JFrame is a JPanel, so you can just add the label and text field to the frame as shown above. – camickr Feb 07 '18 at 15:42
  • Nice catch @camickr I just focused on the main problem and didn't noticed it. I just added an edit following your comment :) – Frakcool Feb 07 '18 at 15:47
  • Thank you @Frakcool , I'm sorry for my bad code, I'am beginner. Your answer was very useful – jjoselon Feb 07 '18 at 15:57
  • It wasn't bad at all, but it could be improved, we all were beginners at some time so keep it up. Just pay attention to suggestions so you can improve :) cheers @jjoselon – Frakcool Feb 07 '18 at 15:58