0

I have been trying to modify the contents of a gridlayouts in a panel i created(Java by the way Ecliplse), but no method ive tried is working this is the code:

// Planning to add about 6 more buttons to complete calculator set.
JPanel panel = new JPanel(new GridLayout(4, 4));
panel.setBounds(0, 150, 400, 500);
panel.setBackground(Color.GRAY);
frame.add(panel);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("10");
// Add buttons to the panel
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b10);
// Tried this but it did not work.
Dimension buttonSize = new Dimension(25, 25);
b1.setPreferredSize(buttonSize);
b2.setPreferredSize(buttonSize);
b3.setPreferredSize(buttonSize);
b4.setPreferredSize(buttonSize);
b5.setPreferredSize(buttonSize);
b6.setPreferredSize(buttonSize);
b7.setPreferredSize(buttonSize);
b8.setPreferredSize(buttonSize);
b9.setPreferredSize(buttonSize);
b10.setPreferredSize(buttonSize);

I just want to reduce the size of the buttons.

camickr
325k21 gold badges174 silver badges293 bronze badges
asked May 27, 2024 at 18:15
2
  • 2
    panel.setBounds(0, 150, 400, 500); And you need to look into the behaviour of a GridLayout with regards to sizing Commented May 27, 2024 at 18:26
  • hey thanks you gave me the answer. Commented May 27, 2024 at 18:36

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.