Question

I'm a beginner and I want to design the gui of the windows calculator in java. I have set some jpanels(Gridlayout) with some jbuttons in each. I set the size of the buttons this way:

jButton[i].setPreferredSize(new Dimension(42, 42));

but buttons in differen jpanels don't have the same size. what should I do?

Was it helpful?

Solution

jButton[i].setPreferredSize(new Dimension(42, 42));

There are 3 easy ways to make buttons larger.

  • Use a big Icon for the button
  • Use a big Font for the button (preferably via a PLAF).
  • Set a large margin around the text and icon.

As to setting the preferred size. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

If you are still intent on going that route, take the advice of @trashgod and override the preferred size instead.

OTHER TIPS

You should also set the same size for your jpanels

ie. jpanel.setBounds(startingWidth, startingHeight, actualWidth, actualHeight);

and then add these to your container panel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top