组件的位置(如何在相同尺寸的中间屏幕上放置几个按钮) [英] Positioning of components (how to place a few buttons center screen same size)

查看:85
本文介绍了组件的位置(如何在相同尺寸的中间屏幕上放置几个按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可靠,推荐的将组件恰好放在JFrame所需位置的方法?

就我而言,我有一个简单的Jframe充当菜单.框架由顶部的标签和中间的三个按钮组成.使用免费设计,这3个按钮似乎漂浮在框架上,当我运行项目时,它们移到了难看的位置.仅将3个按钮拖动到最大长度(到框架的每一侧)时,它们的行为才是锁定到位".这太丑了.我希望将3个按钮集中在框架中间.

我正在使用Netbeans.自由设计是我一直在使用的,但是只有在有很多组件时它们才是好选择,这样它们可以捕捉"到彼此的位置.使用其他集合布局管理器已成功进行.有经验的人将如何处理这样的问题?

我对全面学习感兴趣.如果这个问题是业余的,那么至少有人可以推荐一本教科书或其他GUI学习资源吗? (我已经看过两次使用Java首先实现对象",并且那里只对GUI进行了非常基本的解释).

感谢任何方向.

解决方案

事情很简单,但是当然,您应该自己进行更多搜索.

顺便说一句,您在管理按钮的布局和大小方面存在问题.

请忘记使用任何布局创建器,因为它们会生成混乱的代码,并且不了解实际发生的情况.

通常,对于初学者,我会讨论一种特定的布局,即BoxLayout [当然,有许多更简单的方法可以达到目标,这个答案具有教学目的,并且希望足够大才能被理解,而不仅仅是成为一个在这里,执行此粘贴操作"

首先,我们需要了解Box布局的工作方式.它有两种AXIS(即添加组件的位置的方式),从上到下(PAGE_AXIS)和从左到右(LINE_AXIS).使用这两个轴并嵌套各种布局,您可以随心所欲地完成所有操作.

首先要了解的是如何嵌套更多布局.

要嵌套布局,我们将与JPanel一起使用,每个面板均具有不同的轴,将其放置在另一个内部.

开始分析您的情况. 您在面板中央的三个按钮上有一个标签.我们可以从内部面板开始,其中一个包含组件".我们需要一个PAGE_AXIS(从顶部到botton),其中包括:

// +---------------------+
// |     white space     |
// |                     |
// + - - - - - - - - - - +
// |JLABEL JLABEL  JLABEL|
// + - - - - - - - - - - +
// |     white space     |
// |_____________________|
// |       button        |
// |---------------------|
// |_____________________|
// |       button        |
// |---------------------|
// |_____________________|
// |       button        |
// |---------------------|
// |     white space     |
// +---------------------+

如您所见,面板适合于匹配组件的宽度,其窍门是BoxLayout倾向于为组件提供最大尺寸.在这种情况下,没有边距,因此我们需要一个带有LINE_AXIS的外部不同JPanel,以便放置左右边距,结果将是这样:

// +---+---------------------+---+
// |   |     white space     |   |
// |   |                     |   |
// |   + - - - - - - - - - - +   |
// | W |JLABEL JLABEL  JLABEL| W |
// | H + - - - - - - - - - - + H |
// | I |     white space     | I |
// | T  _____________________  T |
// | E |       button        | E |   
// |    ---------------------    |
// | S  _____________________  S |
// | P |       button        | P |   
// | A  ---------------------  A |
// | C  _____________________  C |
// | E |       button        | E |   
// |    ---------------------    |
// |   |     white space     |   |
// +-----------------------------+

因此,我们首先需要了解的是为每个面板设置布局都很热.

// Initializing
JPanel outside = new JPanel();
JPanel inside = new JPanel();

// setting a layout with horizontal alignment
outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
// setting a layout with vertical alignment
inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));

完成此操作后,我们必须填充面板.从外面开始. 局外人需要(看我的图像)首先是一个水平空间,然后是内部面板,然后是另一个水平空间.我继续添加它们.

// create an horizontal space of 20px
outside.add(Box.createHorizontalStrut(20));
outside.add(inside);
outside.add(Box.createHorizontalStrut(20));

现在我们移到内部,我们需要一个较大的白色空间,一个标签,另一个白色,一个按钮,一个小的白色,该按钮,一个小的白色,第三个按钮和一个大的白色.我继续用这个填充内部面板.

// create a vertical space of 20px
inside.add(Box.createVerticalStrut(20));

JLabel title = new JLabel("THE TITLE");
inside.add(title);

inside.add(Box.createVerticalStrut(20);

JButton btt1 = new JButton("BUTTON ONE");
// create a new dimension object
Dimension d = new Dimension(200,40);
// set the four kind of size, the button CANNOT be different than the dimension I choose
btt1.setSize(d);
btt1.setMinimumSize(d);
btt1.setMaximumSize(d);
btt1.setPreferredSize(d);
JButton btt2 = new JButton("BUTTON TWO");
btt2.setSize(d);
btt2.setMinimumSize(d);
btt2.setMaximumSize(d);
btt2.setPreferredSize(d);
JButton btt3 = new JButton("BUTTON THREE");
btt3.setSize(d);
btt3.setMinimumSize(d);
btt3.setMaximumSize(d);
btt3.setPreferredSize(d);

// Now that the button are ready we put them in the panel.
inside.add(btt1);
inside.add(Box.createVerticalStrut(5));
inside.add(btt2);
inside.add(box.createVerticalStrut(5));
inside.add(btt3);

// Last white space, the bottom margin:
inside.add(Box.createVerticalStrut(20));

现在,我的结构已经完美设置.只需使其可见即可.当然,您需要将其放在JFrame或JDialog中,当然,第一个面板可以是JFrame或JDialog,因为可以为任何组件设置BoxLayout.

通过本基础教程,我希望您了解对这种结构进行编程的基础知识.但是您将需要阅读以下内容,以使事情变得更加复杂: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

祝你有美好的一天.

solid, recommended way of placing components exactly where wanted on a JFrame?

In my case I have a simple Jframe acting as a menu. The frame consists of a label at the top and three buttons in the middle. Using free design the 3 buttons seem to float over the frame and when i run the project they shift into ugly looking positions. Only when the 3 buttons are dragged to maximum length (to each side of the frame) will they behave as "locked in place". This is very ugly.. I would like the 3 buttons centered in the middle of the frame together.

Im using Netbeans. Free design is what I have been using but it is only good when there are alot of components so they can "snap in" to each others positions. Using other set layout managers hasent worked. How would someone with experience deal with such a problem?

I am interested in thorough learning. If this question is painfully amateur could someone at least recommend a text book or other source of GUI learning? (I have been over "Objects first with java" twice and they only have very basic explanations of GUI building in there).

Thanks for any direction.

解决方案

The thing is pretty simple, but of course, you should search a little more by yourself.

By the way, you're is a problem with the managing of the layout and size of the buttons.

Forget please to use any layout creator, because they generate a confuse code and it's not good to understand what really is happening.

Usually for the beginners I talk about a specific layout, that is BoxLayout [edit: of course there are many easier way to reach the goal, this answer have a teaching purpose and want to be large enough to be understood, not just to be a "here, do this copy-paste]

First of all we need to know how Box layout works. It have two kind of AXIS (that is the way you will have the Components added positioned) that are top to bottom (PAGE_AXIS) and left to right (LINE_AXIS). Using this two axis and nesting various layout you can to pretty well whatever you want.

The first thing to know is how to nest more layout.

To nest the layout we will work with JPanel, each with a different axis that will be putted one inside another.

Start analyzing your case. You have a label over three buttons in the center of your panel. We can start from the inner panel, the one with the Components; we need a PAGE_AXIS (top to botton) that will include:

// +---------------------+
// |     white space     |
// |                     |
// + - - - - - - - - - - +
// |JLABEL JLABEL  JLABEL|
// + - - - - - - - - - - +
// |     white space     |
// |_____________________|
// |       button        |
// |---------------------|
// |_____________________|
// |       button        |
// |---------------------|
// |_____________________|
// |       button        |
// |---------------------|
// |     white space     |
// +---------------------+

As you can see the Panel is fitted to match the width of the components, the trick is that BoxLayout prefer to give the component its maximum size. In this case there are no margins, so we need a external different JPanel, with LINE_AXIS, in order to put left and right margins, the result will be this:

// +---+---------------------+---+
// |   |     white space     |   |
// |   |                     |   |
// |   + - - - - - - - - - - +   |
// | W |JLABEL JLABEL  JLABEL| W |
// | H + - - - - - - - - - - + H |
// | I |     white space     | I |
// | T  _____________________  T |
// | E |       button        | E |   
// |    ---------------------    |
// | S  _____________________  S |
// | P |       button        | P |   
// | A  ---------------------  A |
// | C  _____________________  C |
// | E |       button        | E |   
// |    ---------------------    |
// |   |     white space     |   |
// +-----------------------------+

So, the first thing we need to know is hot to set up the layout for each panel.

// Initializing
JPanel outside = new JPanel();
JPanel inside = new JPanel();

// setting a layout with horizontal alignment
outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
// setting a layout with vertical alignment
inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));

Once done this we must fill the panel. Starting from the outside. The outsider need (looking to my image) first an horizontal space, then the inside panel, then another horizontal space. I proceed to add them.

// create an horizontal space of 20px
outside.add(Box.createHorizontalStrut(20));
outside.add(inside);
outside.add(Box.createHorizontalStrut(20));

Now we move to the inside, we need a large white space, the label, another white, a button, a little white, the button, little white, third button and a large white. I proceed to fill inside panel with this.

// create a vertical space of 20px
inside.add(Box.createVerticalStrut(20));

JLabel title = new JLabel("THE TITLE");
inside.add(title);

inside.add(Box.createVerticalStrut(20);

JButton btt1 = new JButton("BUTTON ONE");
// create a new dimension object
Dimension d = new Dimension(200,40);
// set the four kind of size, the button CANNOT be different than the dimension I choose
btt1.setSize(d);
btt1.setMinimumSize(d);
btt1.setMaximumSize(d);
btt1.setPreferredSize(d);
JButton btt2 = new JButton("BUTTON TWO");
btt2.setSize(d);
btt2.setMinimumSize(d);
btt2.setMaximumSize(d);
btt2.setPreferredSize(d);
JButton btt3 = new JButton("BUTTON THREE");
btt3.setSize(d);
btt3.setMinimumSize(d);
btt3.setMaximumSize(d);
btt3.setPreferredSize(d);

// Now that the button are ready we put them in the panel.
inside.add(btt1);
inside.add(Box.createVerticalStrut(5));
inside.add(btt2);
inside.add(box.createVerticalStrut(5));
inside.add(btt3);

// Last white space, the bottom margin:
inside.add(Box.createVerticalStrut(20));

Now I have my structure perfectly set up. Just make it visible and all it's done. Of course you need to put it in a JFrame or a JDialog and of course the first panel can be the JFrame or the JDialog because BoxLayout can be set for any Component.

With this basic tutorial I hope you understood the basics of programming this kind of structures. But you will need to read this in order to go on and make things more complicated: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

have a nice day.

这篇关于组件的位置(如何在相同尺寸的中间屏幕上放置几个按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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