Java 如何设置JButton的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18201495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:15:17  来源:igfitidea点击:

How to set size of JButton?

javaswingjbuttonlayout-managerpreferredsize

提问by Java Curious ?

i am trying to set size of JButton, but by default it take whole frame, it's height easily set but i can't set it's width & why its behaving like that i don't know.

我正在尝试设置 JButton 的大小,但默认情况下它需要整个框架,它的高度很容易设置,但我无法设置它的宽度以及为什么它的行为我不知道。

my code :

我的代码:

    JButton btnNewButton = new JButton("");
    btnNewButton.setPreferredSize(new Dimension(32,0));
    ImageIcon icon = new    ImageIcon(this.getClass().getResource("/images/images_Left.png"));
    btnNewButton.setIcon(icon);
    boxTlacitek.add(btnNewButton);
    getContentPane().add(btnNewButton, BorderLayout.NORTH);

any suggestion please ?

请问有什么建议吗?

采纳答案by MadProgrammer

Change the layout. Try adding the button to another JPanelthen add the panel the frame. BorderLayoutwill stretch the button across the available width of the panel when the component is placed in the NORTHor SOUTHposition

更改布局。尝试将按钮添加到另一个按钮,JPanel然后将面板添加到框架中。 BorderLayout当组件放置在NORTHSOUTH位置时,将在面板的可用宽度上拉伸按钮

enter image description here

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestBorderLayout {

    public static void main(String[] args) {
        new TestBorderLayout();
    }

    public TestBorderLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JButton fat = new JButton("Fat");
                JButton skinny = new JButton("Skinny");

                JPanel buttonPane = new JPanel();
                buttonPane.add(skinny);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(fat, BorderLayout.NORTH);
                frame.add(buttonPane, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

回答by Prabhakaran Ramaswamy

  getContentPane().setLayout(null);
  //setBounds(x,y,width,height)
  btnNewButton.setBounds(10,10,250,100);
  getContentPane().add(btnNewButton);