简体   繁体   English

在Java Swing中放置标签

[英]Positioning a label in Java Swing

I have gone through lots of tutorials which teach about layouts in Java Swing, but they don't seem to suffice my need. 我看过很多教程,这些教程讲授Java Swing中的布局,但是它们似乎不足以满足我的需求。 I am creating a solar system GUI using Java Swing, and i want to place the planets in the GUI according to the values i fetch from my micro controller, which are usually float point values. 我正在使用Java Swing创建一个太阳系GUI,我想根据我从微控制器获取的值(通常是浮点值)将行星放置在GUI中。 I cannot use the Grid Bag Layout, as to position a label i have to specify grid x and grid y, which cannot be the case since I receive float point values from the micro controller. 我不能使用“网格袋布局”,因为要定位标签,我必须指定网格x和网格y,因为我从微控制器接收浮点值,所以不能这样。 The best resource i found is to use absolute layout where i can specify the position of the planet by giving mere X and Y Co-ordinates, which will be fetched from the micro controller. 我发现的最佳资源是使用绝对布局,在其中我可以仅通过给出X和Y坐标来指定行星的位置,而这将从微控制器中获取。 The problem I am facing now is that the absolute layout does not have auto re-size feature. 我现在面临的问题是绝对布局没有自动调整大小功能。

What would be the best possible option to adopt the auto re-size feature in absolute layout? 在绝对布局中采用自动调整大小功能的最佳选择是什么?

Swing tutorials were not generally meant for situations like this -- they were meant for people who want to write more normal GUI applications, using buttons, drop-down boxes, check boxes, radio buttons, menus, and have layout that follows currently accepted practices in terms of positioning those on the screens. Swing教程通常不适用于此类情况,它们适用于希望使用按钮,下拉框,复选框,单选按钮,菜单编写布局并遵循当前公认做法的普通GUI应用程序的人。在屏幕上定位。 If any of that applies to the part of your program that is not displaying planets, I encourage you to use what they have to say about it. 如果其中任何一个适用于您程序中未显示行星的部分,我鼓励您使用他们必须说的内容。

But you want to place things according to calculations of your own. 但是您想根据自己的计算来放置事物。 I recommend doing that in a panel, calculating the size and position of your objects according to the size of the panel at the point of drawing. 我建议在面板中进行此操作,并根据绘图时面板的大小来计算对象的大小和位置。 When the panel resizes, you will need to trap the event that says it is resizing and redraw. 当面板调整大小时,您将需要捕获表明正在调整大小并重新绘制的事件。 You will need to deal with your own minimums and maximums, etc. 您将需要处理自己的最小值和最大值等。

I don't recommend the custom layout manager suggested elsewhere for a couple of reasons. 我不建议出于其他原因在其他地方建议使用自定义布局管理器。 Firstly, it won't save you any work at all -- you are still going to have to write the code that determines positions of things, if you just then draw your own graphic instead of attempting to position a UI element, I think it will actually be less work. 首先,它根本不会为您节省任何工作-您仍然必须编写确定事物位置的代码,如果您只是绘制自己的图形而不是尝试放置UI元素,我认为实际上将减少工作量。 And that's the second reason -- layout managers' purpose is to position UI elements within the panel, and the pieces of your solar system don't really have any need to be UI elements, just graphics on the screen. 这是第二个原因-布局管理器的目的是将UI元素放置在面板中,而您的太阳系的各个部分实际上并不需要UI元素,而只是屏幕上的图形。

Good luck. 祝好运。

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

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