JButton扩展到占据整个框架/容器 [英] JButton expanding to take up entire frame/container

查看:252
本文介绍了JButton扩展到占据整个框架/容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家好我正在尝试使用按钮和标签制作一个swing GUI。即时通讯使用边框布局和标签(在北方字段中)显示正常,但按钮占据框架的其余部分(它在中心字段中)。任何想法如何解决这个问题?

解决方案

你必须将按钮添加到另一个面板,然后将该面板添加到框架。



事实证明,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆