1

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);
}
Community
  • 1
  • 1
Nick Audenaerde
  • 967
  • 2
  • 14
  • 33
  • This is going to be a nasty comment, but... The question reads "I tried to do something using this answer, but I got something else, what to do?!?!" If you want a good answer, show us what you want and what went wrong. "Didn't seem to work out" is not a great description of the problem. Also, if you want specific sizes and position - Why didn't you read the "Absolute positioning" section of the official Oracle tutorial (link in the answer to the question you already read)? – Ordous Jun 11 '14 at 09:44
  • 1
    Added a picture of how it should look like, and the link worked fine, but it did not make you able to positioning yourself – Nick Audenaerde Jun 11 '14 at 09:53

1 Answers1

2

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).

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045