JButton扩展到占据整个框架/容器 [英] JButton expanding to take up entire frame/container
问题描述
你必须将按钮添加到另一个面板,然后将该面板添加到框架。
事实证明,BorderLayout扩展了组件中间的内容
您的代码应如下所示现在:
之前
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信息);
JButton button = new JButton(Ok);
JPanel panel = new JPanel();
panel.add(按钮);
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
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.
It turns out the BorderLayout expands what ever component is in the middle
Your code should look like this now:
Before
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 );
....
}
Change it to something like this:
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);
....
}
Before/After
Before http://img372.imageshack.us/img372/2860/beforedl1.png After http://img508.imageshack.us/img508/341/aftergq7.png
这篇关于JButton扩展到占据整个框架/容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!