Search code examples
javaswingjlabeljtextfieldborder-layout

Place two JLabels above each other with JTextfields next to them


I have this code:

button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.add(label, BorderLayout.LINE_START);
                panel.add(label2, BorderLayout.LINE_START);
                panel.add(textfield, BorderLayout.LINE_END);
                panel.add(textfield2, BorderLayout.LINE_END);
                panel.add(button5);
                panel.revalidate();
                panel.repaint();
                label.setText("Geef de basis van de driehoek in cm: ");
                label2.setText("Geef de hoogte van de driehoek in cm: ");
            }
        });

Which corresponds with this screenshot:

enter image description here

I want it to actually look like this:

enter image description here

I'm new to Java and I know this is probably a dumb question, but I can't figure it out with information on the Internet.

Thanks in advance!


Solution

  • Looking at the code you provided then I see that the top three buttons are not added in this method so I suppose that they are already there:

    In that case lets break your GUI down into bits, like so:

    enter image description here

    We can now make those three left over rechtangles. These rectangles are going to be JPanels each having the corresponding components in them:

    So:

    button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //panel with label and textield
                JPanel base = new JPanel(new BorderLayout());
                base.add(label, Borderlayout.LINE_START);
                base.add(textfield, BorderLayout.LINE_END)
                panel.add(base, BorderLayout.PAGE_START);//top of the page this may interfere with your three buttons I don't know where you are in your layout structure
    
                //panel with label2 and textfield2
                JPanel height = new JPanel(new BorderLayout());
                height.add(label2, BorderLayout.LINE_START);
                height.add(textfield2, BorderLayout.LINE_END);
                panel.add(height, BorderLayout.CENTER);
    
                //button5 doesn't need a panel of it's own as it's only one component
                panel.add(button5, BorderLayout.PAGE_END);
                panel.revalidate();
                panel.repaint();
                label.setText("Geef de basis van de driehoek in cm: ");
                label2.setText("Geef de hoogte van de driehoek in cm: ");
            }
        });
    

    And that should solve your problem, if you have any questions regarding the answer please let me know.

    I hope this helps :)

    EDIT #1:

    Code version 2:

    button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel content = new JPanel(new BorderLayout());
                //panel with label and textield
                JPanel base = new JPanel(new BorderLayout());
                base.add(label, Borderlayout.LINE_START);
                base.add(textfield, BorderLayout.LINE_END)
                content.add(base, BorderLayout.PAGE_START
    
                //panel with label2 and textfield2
                JPanel height = new JPanel(new BorderLayout());
                height.add(label2, BorderLayout.LINE_START);
                height.add(textfield2, BorderLayout.LINE_END);
                content.add(height, BorderLayout.CENTER);
    
                JPanel forbutton5 = new JPanel();
                forbutton5.add(button5);
                content.add(forbutton5, BorderLayout.PAGE_END);
                panel.add(content);
                panel.revalidate();
                panel.repaint();
                label.setText("Geef de basis van de driehoek in cm: ");
                label2.setText("Geef de hoogte van de driehoek in cm: ");
            }
        });