im trying to do kind of nested Panels inside eachother. I'm new to Java so please be patient.
The problem im facing is the pnlLogin
acting wierd. it's showing all 3 panels inside of it only then they don't have a Layout set. whenever i add the Layout none of them appears.
The Nested Panels should look like this:
Jframe (GridLayout)
|
|-> pnlLogin (BoxLayout)
|
|-> pnlInput (BoxLayout)
|-> pnlMsg (BoxLayout)
|-> pnlButtons (BoxLayout)
I also have the following picture to demonstrate how it should look like:
Here is how it looks like when no Layout set:
Here is how it looks like when the got the BoxLayout
What i am doing wrong? how can i solve that?
here is my Code:
import java.awt.*;
import javax.swing.*;
public class AnmeldeFenster {
private JFrame jFrame = new JFrame();
private JPanel pnlLogin = new JPanel();
private JPanel pnlEingabe = new JPanel();
private JPanel pnlMelder = new JPanel();
private JPanel pnlButtons = new JPanel();
public AnmeldeFenster() {
int frameWidth = 500;
int frameHeight = 500;
this.jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.jFrame.setSize(frameWidth, frameHeight);
this.jFrame.setLocation(400, 400);
this.jFrame.setResizable(true);
this.jFrame.setTitle("Anmeldefenster");
this.jFrame.setLayout(new GridLayout());
Container contMngr = this.jFrame.getContentPane();
// Login Panel (Main)
// this.pnlLogin.setBounds(0, 0, frameWidth, frameHeight);
this.pnlLogin.setLayout(new BoxLayout(this.pnlLogin, BoxLayout.PAGE_AXIS));
contMngr.add(this.pnlLogin);
// Eingabe Panel
this.pnlEingabe.setBackground(Color.BLACK);
this.pnlEingabe.setLayout(new BoxLayout(this.pnlEingabe, BoxLayout.PAGE_AXIS));
this.pnlEingabe.setPreferredSize(new Dimension(frameWidth, 300));
this.pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlEingabe);
// Melder Panel
this.pnlMelder.setBackground(Color.GREEN);
this.pnlMelder.setLayout(new BoxLayout(this.pnlMelder, BoxLayout.PAGE_AXIS));
this.pnlMelder.setPreferredSize(new Dimension(frameWidth, 100));
this.pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlMelder);
// Button's Panel
this.pnlButtons.setBackground(Color.BLUE);
this.pnlButtons.setLayout(new BoxLayout(this.pnlButtons, BoxLayout.PAGE_AXIS));
this.pnlButtons.setPreferredSize(new Dimension(frameWidth, 100));
this.pnlButtons.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlButtons);
this.jFrame.setVisible(true);
}
public static void main(String[] args) {
new AnmeldeFenster();
}
}
The design in ASCII Draw
┌─┬───────────────────────────────────────┬─┐
│ ├───────────────────────────────────────┤ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ height 200 │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ └───────────────────────────────────────┘ │ height 500
│ empty place │
│ ┌───────────────────────────────────────┐ │
│ │ height 100 │ │
│ │ │ │
│ └───────────────────────────────────────┘ │
│ empty place │
│ ┌───────────────────────────────────────┐ │
│ │ height 100 │ │
│ │ │ │
│ ├───────────────────────────────────────┤ │
└─┴───────────────────────────────────────┴─┘
width 400