How do I get the button sizes in this GridLayout + FlowLayout to work

This program combines a GridBagLayout north panel ofr customer data, and a FlowLayout west panel for button group on one JFrame f.  But I cannot set the button size so that they would be even. How can I do that, please?
package formslayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayout_FlowLayout {
	
  public static void main(String[] args) {

	  JPanel p1 = new JPanel();

    // COMPONENT SETUP
	  JTextField customer = new JTextField(10);
	  JTextField address = new JTextField(10);
	  JButton b1 = new JButton("  Ok  ");
	  JButton b2 = new JButton("Cencel");
	  JButton b3 = new JButton(" Exit ");
	  b1.setSize(150,150);
	  b2.setSize(150,150);
	  b3.setSize(150,150);

	  //GRID BAG LAYOUT SETUP
	  GridBagConstraints c1 = new GridBagConstraints();
	  p1.setLayout(new GridBagLayout());
	  c1.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c1.gridx = 1;  // column
	  c1.gridy = 1;	// row
	  p1.add(customer, c1); // constraints passed in
	  c1.gridx = 1;
	  c1.gridy = 2;
	  p1.add(address, c1); // constraints passed in
	  
	  //PANEL SETUP
	  JPanel p2 = new JPanel(new FlowLayout());
	  p2.add(b1);
	  p2.add(b2);
	  p2.add(b3);
	  p1.setBackground(Color.blue);
	  p2.setBackground(Color.black);
	  p1.setSize(200,100);
	  p2.setSize(200,300);
	  
	  //FRAME SETUP
	  JFrame f = new JFrame();
	  f.setResizable(false);
	  f.getContentPane().add(p1, BorderLayout.NORTH);
	  f.getContentPane().add(p2, BorderLayout.WEST);
	  f.pack();
	  f.setVisible(true);
  }
}

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vanavah EdwardsAuthor Commented:
I had to add spaces in front and after of the Ok text to get it even to cancel but this affects the action performer
0
for_yanCommented:
How can it affect action ?

You can set any text  on the button - it does not affect the action.

Show the code where it affected the action
0
Vanavah EdwardsAuthor Commented:
If you look at line 25-27 above, it has spaces to even out the button sizes.  In the actionPerformer, I must do the same thing when testing for selection.  Can the button sizes be set properly in the above code?  
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

for_yanCommented:
Post your code where you need "do the same thing" - I don't understadn what you mean.
0
CEHJCommented:
Try the following
int maxWidth = Math.max(b1.getPreferredSize().width, Math.max(b2.getPreferredSize().width, b3.getPreferredSize().width));
Dimension size = new Dimension(maxWidth, b1.getPreferredSize().height);
b1.setPreferredSize(size);
b2.setPreferredSize(size);
b3.setPreferredSize(size);
p2.add(b1);
p2.add(b2);
p2.add(b3);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vanavah EdwardsAuthor Commented:
Problem solved.  This lines of code works well.  Why?  I am looking not just for a solution but I am seeking understanding in everything in Java.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.