Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top