简体   繁体   中英

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?

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM