Hello Members,

How do I change the code below sothat I can have the JLabel at the center of the JFrame and the two JButtons directly below the JLabel?

Thank you!

import java.awt.*;
import javax.swing.*;

public class Layout {


        public static void main (String args[])
            {
                JFrame m = new JFrame("Testing");
               
                JButton p = new JButton ("Button_1");
                JButton q = new JButton ("Button_2");
                JLabel r = new JLabel();
                r.setText("Enter your value here");

                m.add(r);
                m.add(p);
                m.add(q);
                m.setSize(500, 500);
                m.setVisible(true);
                m.getContentPane().setLayout(new FlowLayout());
            }
  }

Recommended Answers

All 5 Replies

1. Create a panel for the buttons:

JPanel buttonPanel = new JPanel();

2. Add your buttons to the buttonPanel
3. Give the frame a border layout:

m.setLayout(new BorderLayout());

4. Add the label to the center of the frame:

m.add(r, BorderLayout.CENTER);

5. Add the buttons to the bottom of the frame:

m.add(buttonPanel, BorderLayout.SOUTH);

6. get rid of the line (21) that sets the layout to FlowLayout

Hello Kramerd,

As per your reply, I tested the following code:

import java.awt.*;
import javax.swing.*;

public class Layout {


        public static void main (String args[])
            {
                JFrame m = new JFrame("Testing");
                JPanel buttonPanel = new JPanel();
                JButton p = new JButton ("Button_1");
                JButton q = new JButton ("Button_2");
                JLabel r = new JLabel();
                r.setText("Enter your value here");
                
                buttonPanel.add(p);
                buttonPanel.add(q);
                m.setLayout(new BorderLayout());
                m.add(r, BorderLayout.CENTER);
                m.add(buttonPanel, BorderLayout.SOUTH);
                m.setSize(200, 200);
                m.setVisible(true);
               
            }
  }

The JLabel seems to be left justified rather being at the center. The program gave me the same output for both

m.add(r, BorderLayout.CENTER);

and

m.add(r, BorderLayout.WEST);

Anyway to fix that?

Also, there seems to be a quite a gap between the JLabel and the JButtons. Is there anyway to have the two JButtons right below the (on the next line) JLabel?

Thank you so much!!

use the same code
nut put the JLabel on a panel also

As commingking says, you can put the JLabel in a panel also, which will center the label by default.

To reduce the gap between the label and the buttons, the easiest fix is to simply resize the window. But if for some reason you need the height and still want the buttons closer to the label, you can use a different layout manager.

Try setting the layout on your frame to FlowLayout, and then you can just add the 2 panels (without BorderLayout.SOMETHING).

Hello kramerd and king,

Thanks a lot for the help!! I will surely get back to you once I test the program.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.