tutyx
tutyx

Reputation: 13

Change size of JButtons

I have a very big problem. I need to do vertical menu in the center of window. What could be easier? What I did:

  1. I create JFrame and set BorderLayout to it: JFrame jfr = new JFrame("Frame");
  2. Then I create 4 buttons:
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JButton b3 = new JButton("b3");
JButton b4 = new JButton("b4");
  1. I created panel and add all buttons to panel:
JPanel jpan = new JPanel();
jpan.setLayout(new BoxLayout(jpan, BoxLayout.Y_AXIS));
jpan.add(b1);
jpan.add(b2);
jpan.add(b3);
jpan.add(b4);
  1. Aligned all buttons
b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b4.setAlignmentX(JComponent.CENTER_ALIGNMENT);
  1. And add panel to JFrame
jfr.add(jpan, BorderLayout.CENTER);

Please help me to understand this layouts! Only say like this: "You should use this, when you use this layout" And now main question: How can I change size of buttons?

Upvotes: 1

Views: 68

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

There are a number of easy ways to change the size of buttons:

  1. Give the buttons an icon of a different size.
  2. Make the font of the buttons a different size.
  3. Set more / less space between the button contents (the icon and text) and the border of the button.
  4. Give the buttons more / less text.

Given the last is quite arbitrary, here is a demonstration of the first 3 techniques:

enter image description here

Upvotes: 1

Related Questions