문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top