简体   繁体   English

如何自动调整Java Swing元素的大小?

[英]How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. 我正在尝试自动调整应用程序左侧的大小。 I have a JTextField and a JTree on the left and 3 JButtons on the right. 我在左侧有一个JTextField和JTree,在右侧有3个JButton。 But I just don't know how to make the left side auto resizeable. 但是我只是不知道如何使左侧自动调整大小。

I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. 我是使用Netbeans GUI Creator(或任何称为它的)来完成的,但是我不知道没有Netbeans怎么做。 (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing. (我通常不使用Netbeans进行编程,这只是一个例外,看看是否可以使用Swing进行编程。

Here is the code Netbeans created: http://pastebin.com/ERwY4rBC It's not that the code is completely unusable but I wanted to try it manually. 这是Netbeans创建的代码: http : //pastebin.com/ERwY4rBC不是代码完全不可用,而是我想手动尝试。

The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. GroupLayout看起来不错,但是Oracle网站说它主要是供GUI工具使用。 So, using GroupLayout would be not "Java like" or how do I have to understand it? 因此,使用GroupLayout不会像Java那样,或者我必须如何理解它? Or is there even a better way to achieve this without GroupLayout? 还是没有GroupLayout甚至有更好的方法来实现这一目标?

Thanks! 谢谢!

So, using GroupLayout would be not "Java like" or how do I have to understand it 因此,使用GroupLayout不是“像Java一样”,或者我必须如何理解它

GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. GroupLayout确实使手工编码变得非常困难,并且导致产生大量代码。 But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose. 但这不是“不像Java”,它不是您想要手工完成的事情,并且之后的代码非常冗长,因此很难阅读。

What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts' . 您可以尝试使用某些“嵌套布局”轻松实现(根据屏幕截图)。 If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior. 如果您的主面板使用BorderLayout ,则将左侧的可调整大小的面板放置在BorderLayout.CENTER ,将另一个不可调整的面板放置在BorderLayout.EAST您将获得所需的调整大小行为。

Then you just have to decide which LayoutManager to use for those individual panels. 然后,您只需要决定对那些单独的面板使用哪个LayoutManager I think that both the BoxLayout as well as the FlowLayout will do just fine. 我认为BoxLayoutFlowLayout都可以。

Do yourself a favour and use MigLayout for all your layout needs. 帮自己一个忙,并使用MigLayout满足您的所有布局需求。 It is especially convenient for coding UI by hand. 手动编码UI特别方便。

There is a WebStart application on their site that demos different layout situations with code samples provided. 他们网站上有一个WebStart应用程序,通过提供的代码示例演示了不同的布局情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM