为什么BoxLayout不允许我改变JButton的宽度但让我改变高度? [英] Why will BoxLayout not allow me to change the width of a JButton but let me change the height?

查看:154
本文介绍了为什么BoxLayout不允许我改变JButton的宽度但让我改变高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取我的 JDialog 的布局,以适应我移植到Java的程序的特定外观,我已经之前使用过几个LayoutManagers取得了巨大的成功,但由于某些原因我似乎无法完成这项工作。我的目标是让 JDialog 的右(东)侧按照自上而下的顺序包含查找下一个和取消按钮,然后是下面的任何额外空格这两个按钮总是位于 JDialog 的顶部,但由于某种原因 BoxLayout 不断忽略任何尝试在改变(这是我丢失的地方) JButton 的宽度。代码如下。

I'm trying to get the Layout of a JDialog of mine to fit a particular look that a program in which I'm porting to Java has, I've used several LayoutManagers before with great success yet for some reason I cannot seem to get this working at all. My goal is to have the Right (East) side of the JDialog contain a "Find Next" and "Cancel" button in a top-down order and then any extra space below so that the two buttons are always at the top of the JDialog, yet for some reason BoxLayout is continously ignoring any attempts at changing (this is where I'm lost) the width of a JButton. Code follows.

JButton findNext = new JButton("Find Next");
JButton cancel = new JButton("Cancel");
cancel.setPreferredSize(new Dimension((int)findNext.getPreferredSize().getWidth(),  
    (int)cancel.getPreferredSize().getHeight()));

JPanel example = new JPanel();  
example.setLayout(new BoxLayout(example, BoxLayout.Y_AXIS));  
example.add(findNext);
example.add(cancel);  
example.add(Box.createGlue());  

无论我尝试什么,取消总是保持正常的尺寸。我尝试了 setMinimumSize() setMaximumSize(),其参数与 setPreferredSize <相同/ code>没有运气。我甚至尝试过 cancel.setPreferredSize(new Dimension(500,500)); 并且按钮高度是唯一调整的东西,它仍然保留了它的默认宽度。

No matter what I try, cancel always retains it's normal size. I've tried setMinimumSize() and setMaximumSize() with the same parameters as setPreferredSize with no luck. I've even tried cancel.setPreferredSize(new Dimension(500, 500)); and the buttons height was the only thing adjusted, it STILL retained the default width it was given.

要清除任何问题,这就是它的样子(现在我已经完成了),你会看到查找下一个和取消 按钮的大小不同。

To clear up any questions, here is what it looks like (now that I've finished it) and you'll see that the "Find Next" and "Cancel" buttons are not the same size.

推荐答案

我知道这是一个老问题,但我真的没有看到一个很好的解释。因此,为了偶然发现这一点的搜索者,我将添加两美分。

I know this is an old question but I don't really see a good explanation. So for the sake of searchers that stumble upon this I will add my two cents.

在Swing中调整组件大小有三种方法:setPreferredSize(),setMinimumSize()和setMaximumSize()。然而,重要的一点是,由特定的布局管理器决定是否遵守这些方法中的任何一种。

There are three methods associated with sizing components in Swing: setPreferredSize(), setMinimumSize(), and setMaximumSize(). However, the important point is that it is up to the particular layout manager being used as to whether or not it honors any of these methods.

对于BoxLayout(布局,原始海报正在使用):

For BoxLayout (the layout the original poster is using):


  • setMinimumSize() - BoxLayout 尊重此

  • setMaximumSize() - BoxLayout 尊重此

  • setPreferredSize() - 如果X_AXIS是被使用的宽度是值得尊敬的,如果正在使用Y_AXIS高度被尊重

  • setMinimumSize() -- BoxLayout honors this
  • setMaximumSize() -- BoxLayout honors this
  • setPreferredSize() -- if X_AXIS is being used width is honored, if Y_AXIS is being used height is honored

OP正在使用Y_AXIS BoxLayout这就是为什么只有他的高度正在改变。

The OP is using a Y_AXIS BoxLayout which is why only his height was being changed.

更新:我为所有布局管理器整理了一个包含相同信息的页面。希望它可以帮助一些搜索者: http://thebadprogrammer.com/swing-layout-manager-调整大小/

Update: I put together a page with this same information for all of the layout managers. Hopefully it can help some searchers out: http://thebadprogrammer.com/swing-layout-manager-sizing/

这篇关于为什么BoxLayout不允许我改变JButton的宽度但让我改变高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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