Nick Audenaerde
Nick Audenaerde

Reputation: 1025

How to add multiple JPanels to JFrame with different sizes

I tried to do this from stackoverflow:

adding multiple jPanels to jFrame

But that didn't seem to work out like in the example, could anyone tell me what im doing wrong? Im trying to add multiple JPanels with each their own sizes to the JFrame. I was also hoping it was possible to give each JPanel specific sizes and ability to put them on the exact spot i want.

Picture of what i try to make: Mockup

This is my code so far: public ReserveringenGUI(ReserveringController controller) { this.controller = new ReserveringController(); makeFrame(); }

public void makeFrame() {
    JFrame frame1 = new JFrame();
    frame1.setTitle("Reserveringen");
    frame1.setSize(800, 500);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel container = new JPanel();
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

    JPanel willekeurigPanel = new JPanel();
    willekeurigPanel.setSize(400, 500);
    willekeurigPanel.setBackground(Color.YELLOW);
    willekeurigPanel.setVisible(true);

    JPanel overzichtPanel = new JPanel();
    overzichtPanel.setSize(400, 500);
    overzichtPanel.setBackground(Color.red);
    overzichtPanel.setVisible(true);

    DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    DateFormatter df = new DateFormatter(format);
    JFormattedTextField dateBeginField = new JFormattedTextField(df);
    dateBeginField.setPreferredSize(new Dimension(250, 20));
    dateBeginField.setValue(new Date());

    JFormattedTextField dateEndField = new JFormattedTextField(df);
    dateEndField.setPreferredSize(new Dimension(250, 20));
    dateEndField.setValue(new Date());

    JTextField klantnummer = new JTextField();
    klantnummer.setPreferredSize(new Dimension(250, 20));
    JTextField artikelnummer = new JTextField();
    artikelnummer.setPreferredSize(new Dimension(250, 20));

    JLabel dateBeginLabel = new JLabel("Begin Datum ");
    JLabel dateEndLabel = new JLabel("Eind datum: ");
    JLabel klantID = new JLabel("Klant nummer: ");
    JLabel artikelID = new JLabel("Artikel nummer: ");

    JButton voegReserveringToe = new JButton("Voeg toe");

    voegReserveringToe.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            voegReserveringToeActionPerformed(evt);
        }
    });

    willekeurigPanel.add(dateBeginLabel);
    willekeurigPanel.add(dateBeginField);

    willekeurigPanel.add(dateEndLabel);
    willekeurigPanel.add(dateEndField);

    willekeurigPanel.add(klantID);
    willekeurigPanel.add(klantnummer);

    willekeurigPanel.add(artikelID);
    willekeurigPanel.add(artikelnummer);

    willekeurigPanel.add(voegReserveringToe);



    container.add(willekeurigPanel);
    container.add(overzichtPanel);

    frame1.add(container);
    frame1.setVisible(true);
}

Upvotes: 1

Views: 2983

Answers (1)

trashgod
trashgod

Reputation: 205775

As discussed here, don't set the size and position of components arbitrarily. Instead, let the layout do the work, nesting as required. Use the GroupLayout shown here for the labeled input fields. Add each to the CENTER of a panel having BorderLayout, with a button in the SOUTH on the left. Finally, add both panels to an enclosing panel having GridLayout(1, 0).

Upvotes: 2

Related Questions