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 扩展以占据整个框架/容器


基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01