Java 设置 JButton 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25945439/
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
Setting JButton size
提问by Rostro
I'm trying to resize JButton to always be a certain size. I have 9 of these buttons. I understand from the API that it JButton inherits setSize(int a, int b) and setSize(Dimension d). I opted to use the second though I tried the other and it didn't fix my problem. Here is the code.
我正在尝试将 JButton 调整为始终为特定大小。我有 9 个这样的按钮。我从 API 中了解到它 JButton 继承了 setSize(int a, int b) 和 setSize(Dimension d)。我选择使用第二个,尽管我尝试了另一个,但它没有解决我的问题。这是代码。
// setup buttons
reset = new JButton("Reset");
square1 = new JButton();
square2 = new JButton();
square3 = new JButton();
square4 = new JButton();
square5 = new JButton();
square6 = new JButton();
square7 = new JButton();
square8 = new JButton();
square9 = new JButton();
//set button size
Dimension d = new Dimension(100,100);
square1.setSize(d);
square2.setSize(d);
square3.setSize(d);
square4.setSize(d);
square5.setSize(d);
square6.setSize(d);
square7.setSize(d);
square8.setSize(d);
square9.setSize(d);
I've tried several diferent dimensions and none of them make any difference. What am I missing? I'm using a gridLayout(3,3,5,5) for the the JPanel that the buttons are on. The dimensions for the JFrame are (400,425). Thanks for any help!
我尝试了几个不同的维度,但没有一个有任何区别。我错过了什么?我正在为按钮所在的 JPanel 使用 gridLayout(3,3,5,5)。JFrame 的尺寸为 (400,425)。谢谢你的帮助!
采纳答案by Hovercraft Full Of Eels
The layout managers usually don't respect the size of a component, so setting size often has little effect. Instead, the layout managers usually respect the preferredsizes of components. Having said this, it's usually better to not try to set the preferredSizes yourself via setPreferredSize(Dimension d)
but rather to let the layout managers and the components do this for you. If you must do it yourself, consider extending the component and overriding getPreferredSize()
and adding smartcode that returns a decent and proper preferredSize Dimension.
布局管理器通常不考虑组件的大小,因此设置大小通常影响不大。相反,布局管理器通常尊重组件的首选大小。话虽如此,通常最好不要尝试自己设置 preferredSizes,setPreferredSize(Dimension d)
而是让布局管理器和组件为您完成此操作。如果您必须自己做,请考虑扩展组件并覆盖getPreferredSize()
和添加智能代码,以返回合适的首选尺寸维度。
For example, please have a look at my code in my answer to a recent question here. In it, I change the size of the JButton's Font to make it larger, but i never set the sizes or preferredSizes of any component, but rather let the components themselves and the container's layout managers do this work for me.
例如,请在此处查看我对最近问题的回答中的代码。在其中,我更改了 JButton 字体的大小以使其更大,但我从未设置任何组件的大小或首选大小,而是让组件本身和容器的布局管理器为我完成这项工作。
If you don't believe me, please read some of the posts and comments by kleopatra, one of the smartest and most influential Swing coders that I know of, and one of the authors of the SwingX toolkit. For example her answer here.
如果您不相信我,请阅读kleopatra 的一些帖子和评论,他是我所知道的最聪明、最有影响力的 Swing 编码员之一,也是SwingX 工具包的作者之一。例如她在这里的回答。
回答by Jon
Try using square1.setPreferredSize(d)
instead of setSize(d)
.
尝试使用square1.setPreferredSize(d)
代替setSize(d)
.
回答by Priyank
You can use setBounds() method as:
您可以将 setBounds() 方法用作:
Frame f=new Frame();
f.setLayout(null);
Button b=new Button("Dummy");
//setBounds() methods accepts 4
// integer parameters
/* 1. Location on x axis
2. Location on y axis
3. Width
4. Height */
b.setBounds(20,30,50,30);
f.add(b);
Thats how you set size for a button There are more ways to specify size but i use this one Hope this will help you....
这就是你设置按钮大小的方式 有更多的方法可以指定大小,但我使用这个希望这会帮助你....