JButton expanding to take up entire frame/container(JButton 扩展以占据整个框架/容器)
问题描述
大家好.我正在尝试制作一个带有按钮和标签的摇摆 GUI.我使用边框布局,标签(在北场)显示得很好,但按钮占据了框架的其余部分(它在中心场).知道如何解决这个问题吗?
Hey everyone. I'm trying to make a swing GUI with a button and a label on it. im using a border layout and the label ( in the north field ) shows up fine, but the button takes up the rest of the frame (it's in the center field). any idea how to fix this?
推荐答案
您必须将按钮添加到另一个面板,然后将该面板添加到框架中.
You have to add the button to another panel, and then add that panel to the frame.
原来 BorderLayout 扩展了中间的组件
It turns out the BorderLayout expands what ever component is in the middle
您的代码现在应该如下所示:
Your code should look like this now:
之前
public static void main( String [] args ) {
JLabel label = new JLabel("Some info");
JButton button = new JButton("Ok");
JFrame frame = ...
frame.add( label, BorderLayout.NORTH );
frame.add( button , BorderLayout.CENTER );
....
}
把它改成这样:
public static void main( String [] args ) {
JLabel label = new JLabel("Some info");
JButton button = new JButton("Ok");
JPanel panel = new JPanel();
panel.add( button );
JFrame frame = ...
frame.add( label, BorderLayout.NORTH );
frame.add( panel , BorderLayout.CENTER);
....
}
之前/之后
http://img372.imageshack.us/img372/2860/beforedl1.png之前http://img508.imageshack.us/img508/341/aftergq7.png之后
这篇关于JButton 扩展以占据整个框架/容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JButton 扩展以占据整个框架/容器


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01