简体   繁体   English

Java AWT setLayout(null)似乎不起作用

[英]Java AWT setLayout(null) doesn't seem to be working

I'm trying to re-purpose an existing Java AWT (stand alone) application to run on dedicated, single-purpose hardware (think a kiosk in a museum that also controls hardware behind the scenes) and my presumption that if I simply set the layout manager on my main panel to null I'd be able to lay out items using something like Rectangle(starting x, starting y, x-width, y-height) or perhaps another similar method to position things, has proven false! 我试图重新利用现有的Java AWT(独立)应用程序,使其在专用的单用途硬件上运行(想想博物馆中的售货亭也可以在后台控制硬件),并且我的假设是,如果我简单地设置我的主面板上的布局管理器为null我将能够使用Rectangle(starting x, starting y, x-width, y-height)类的东西或其他类似的方法来放置物品,这证明是错误的! So, I'm more lost than I thought I would be! 所以,我比我原本想的要迷失了!

Here are a few excerpts: 以下是一些摘录:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
[...]
public class myGUI extends JFrame
{
  JPanel MainPanel = new JPanel();
  JMenuBar Menu = new JMenuBar();
  int MaxWidth = 1920;
  int MaxHeight = 1080;
  Dimension FullScreen = new Dimension(MaxHeight, MaxWidth);
  Rectangle recHZbar = new Rectangle(0, 32, MaxWidth, 4);
[...]
    MainPanel.setLayout(null);
    MainPanel.setPreferredSize(FullScreen);
    MainPanel.setEnabled(true);
    MainPanel.setBackground(LightBlue);

There are all manner of components totaling around a hundred or so and it makes no sense to present them here. 各种各样的组件合计大约有一百个左右,在这里展示它们是没有意义的。 Suffice to say that I'm trying to eliminate what were stand-alone frames and instead present all the data around the edges of a very large screen and then manage the center space of the screen separately with key data, hopefully able to use visibility to switch what the user sees (instead of panes / panels), since in many cases there's a lot of commonality. 可以说我正在尝试消除独立帧,而是在非常大的屏幕边缘周围显示所有数据,然后使用关键数据分别管理屏幕的中心空间,希望能够使用可见性来切换用户看到的内容(而不是窗格/面板),因为在许多情况下存在很多共性。

I thought that by setting the layout manager to null I would then be able to position components on MainPanel using something like this horizontal bar with a message embedded in it: 我认为,通过将布局管理器设置为null,我便可以使用类似此水平条并在其中嵌入消息的方式在MainPanel上定位组件:

JLabel HorizontalBar = new JLabel();
HorizontalBar.setBackground(DarkBlue);
HorizontalBar.setFont(new java.awt.Font("Dialog", 0, 10));
HorizontalBar.setForeground(LightBlue);
HorizontalBar.setPreferredSize(dimMxW10pt);
HorizontalBar.setOpaque(true);
HorizontalBar.setHorizontalTextPosition(SwingConstants.CENTER);
HorizontalBar.setText(HZBarTxt);
HorizontalBar.setBounds(recHZbar);
MainPanel.add(HorizontalBar, null);

... And this, of course, works, BUT, when I tried to position this horizontal bar (via setBounds(rectangle) ), it's apparent that the coordinates are based off of the bottom of the JMenuBar I added earlier, and NOT from the upper left corner of the screen! ...这当然是有效的,但是,当我尝试定位此水平条时(通过setBounds(rectangle) ),显然坐标是基于我先前添加的JMenuBar底部的,而不是基于屏幕的左上角! This has me rather concerned! 这让我颇为关注! (I presume the next bar will be based on the space below the first one, etc?!) Am I correct in thinking I've got a layout manager I didn't (explicitly) ask for? (我认为下一个栏将基于第一个栏的下方空间,等等?!)我是否认为自己有一个(不是(明确)要求的)布局管理器是否正确? (If so, how do I avoid it?) (如果是这样,如何避免呢?)

I'm hoping I'm overlooking something simple to be able to do positioning myself without having to go through too much work. 我希望我能忽略一些简单的事情,以便能够定位自己而不需要进行过多的工作。 If I can't just pick where I want things to be on the screen, I'm going to be in trouble on this project! 如果我不能只选择要在屏幕上显示的内容,那么这个项目将会很麻烦! I'm hoping to avoid lots of little panels and such. 我希望避免使用很多小面板等。 I need to create irregular columns and so forth. 我需要创建不规则的列等等。 I know I can do the math to lay things out how I want, and I'm loathe to trust a layout manager to get it right, especially since the testing on the actual production hardware is very hard, and if the layout manager is different, it'll mean trouble. 我知道我可以进行数学运算以按照自己的意愿布置事物,并且我不愿意信任布局管理器来正确地进行它,尤其是因为在实际生产硬件上的测试非常困难,并且如果布局管理器是不同的,那就意味着麻烦。 I may well be I'm overlooking the right layout manager - the "do it yourself layout manager", perhaps? 我很可能会忽略合适的布局管理器-也许是“自己做布局管理器”吗? - but I don't see how "GridLayout" is going to work for me, at least, not easily. -但我看不到“ GridLayout”如何为我工作,至少不容易。 So I'm hoping to learn how to do my own layout as simply and directly as possible (which is what I thought I was already doing). 因此,我希望学习如何尽可能简单和直接地进行自己的布局(这就是我以为我已经在做的事情)。

TIA. TIA。

It turns out that my original assumption that the call to setLayout(null) hadn't worked was itself mistaken, and that's a good thing! 事实证明,我最初的假设认为对setLayout(null)的调用setLayout(null) ,这本身就是一个错误,这是一件好事!

Given MY requirements, I made the exactly correct choice of NOT using a window manager at all. 根据我的要求, 我做出了完全不使用窗口管理器的完全正确的选择。 Yes, it can easily be seen from the comments to the question that many people think it's foolhearty or even stupid to NOT use a window manager, but in my case I made the EXACTLY right choice, and here's why: 是的,从问题的评论中可以很容易地看出,很多人认为不使用窗口管理器是愚蠢的,甚至是愚蠢的,但是在我的情况下,我做出了完全正确的选择,这就是为什么:

THIS PROJECT'S circumstances are one of the rare cases for "doing it yourself", and, indeed, if I'd used a window manager it would NOT have worked out _AT_ALL!_ ...at least not in the time I had available. 这个项目的情况是“自己动手做”的罕见情况之一,而且,实际上,如果我使用了窗口管理器,它至少不可能在_AT_ALL!_的基础上解决。

A brief review of the use-case for this project 对该项目用例的简要回顾

This was for dedicated hardware control and, indeed, it could not run in production in any place but a very singular installation of specialized hardware that the Java code is providing a user-interface for. 这是用于专用硬件控制的,实际上,它不能在生产中的任何地方运行,只能安装专用的非常特殊的专用硬件,而Java代码可以为其提供用户界面。 Further, it won't have any internet connection, ever, and will never be upgraded. 此外,它永远不会有任何互联网连接,也永远不会升级。 There's ZERO concern over either operating system or other software upgrade - it just cannot happen. 对于操作系统或其他软件升级存在零担忧-只是不可能发生。 As it has a singular fixed running environment, there's no concern over font-change handling or anything like that. 由于它具有单一的固定运行环境,因此无需担心字体更改处理或类似问题。

The Application Design; 应用设计; WHY A Layout Manager Would Screw It Up 为什么布局管理器会搞砸

I chose to use one frame, "undecorated" so that if fills the entire display, like this: 我选择使用“未装饰”一帧,以便在整个显示中都可以像这样:

布局管理器无法完成此工作的示例。

This fills a 1090 X 1920 display completely, so IDK how well it will go here. 这完全填满了1090 X 1920显示屏,因此IDK在这里效果如何。

I created a JFrame that serves as a backdrop for the whole thing. 我创建了一个JFrame作为整个背景。 Within it, I first created a menu bar, followed by a heading / title bar, and for these things, a layout manager could have done a great job, of course, but that's the end of the easy part. 在其中,我首先创建了一个菜单栏,然后​​创建了一个标题/标题栏,对于这些事情,布局管理器当然可以做得很好,但这只是简单部分的结尾。

I created a right and left column of necessarily different widths and then a section at the bottom just by placing the items using item.setBounds(X, Y, W, H) . 通过使用item.setBounds(X, Y, W, H)放置项目,我创建了一个左右宽度必定不同的列,然后在底部创建了一个区域。 I used variables for the values, and used them to create standard row & column positions, widths and heights. 我使用变量作为值,并使用它们来创建标准的行和列位置,宽度和高度。 This provided for easy shifting from the standard in places that required it; 这使得在需要的地方可以轻松地从标准中转移出来; I'd just use a different set of variables (using a naming convention I invented to keep it easy). 我只是使用一组不同的变量(使用为方便起见而发明的命名约定)。 I'd imagine that you COULD have used a layout manager for this part, but it would have been tedious and it's not at all clear it would have been any less work. 我以为您可能已经在这部分中使用了布局管理器,但是这将是乏味的,并且完全不清楚它会减少工作量。 In particular, how do you get the two vertical columns do be where you want them? 特别是,如何使两个垂直列位于所需的位置? You'd have to create separate inset panes / panels for each differently formatted region within each column - and even the bottom rows! 您必须为每一列中甚至底部的行中每个格式不同的区域创建单独的插图窗格/面板! You'd have to get them to stack or space just right, too. 您还必须使它们正确堆叠或间隔。 Then there are those vertical and horizontal bars - how'd you do that? 然后是那些竖线和横线-您是怎么做到的?

Vitally, I left room for an inset panel in the center, of which there are a VERY large number (!!) way more than are apparent from what you can see in the sample image. 实际上,我为中间的插入面板留出了空间,其中有一个非常大的数字(!!),远远超出了从示例图像中所看到的。 They're JPanels , only one of which is visible here, of course. 它们是JPanels ,当然这里只有其中一个可见。 And this is where a layout manager would completely fall on its face. 这就是布局管理器完全落在脸上的地方。 Good UI design keeps things consistent and so users can know where to expect to find various things. 好的UI设计可以使事物保持一致,因此用户可以知道在哪里可以找到各种事物。 And on the various panels, there are things that are common among some panels and different on others with DIFFERENT commonality, and there are yet more panels that cross with commonality between different sets of panes, yet few of the panels are really full enough that the common layout manager packing algorithms could handle; 在不同的面板上,有些面板之间共有一些共同点,而其他面板上的共同点却有所不同,并且有更多的面板在不同组的窗格之间相互交叉,但是很少有面板足够满,以至于常见的布局管理器打包算法可以处理; Various items are - and need to be - in what may appear to be non-standard positions for various reasons, so using a layout manager would have required filling up the panes with lots of sub-panels and such so that each of the various layout managers could do their jobs properly. 由于各种原因,各种项目都处于-并且需要处于-非标准位置,因此使用布局管理器将需要在窗格中填充许多子面板,以使各个布局中的每个管理人员可以正确地完成工作。 By NOT using a layout manager, and thereby being free to just exactly specify the different positioning of the components when looking at different inset panes, I was able to VERY SIMPLY just use panel.add(item) syntax to move items between panes and keep the position exactly. 通过不使用布局管理器,从而可以自由地在查看不同的嵌入式窗格时完全指定组件的不同位置,因此我能够非常简单地使用panel.add(item)语法在窗格之间移动项目并保持位置准确。 Further, because of the same top position of the outer panel and the inner ones, getting rows exactly right was a cinch! 此外,由于外部面板和内部面板的顶部位置相同,因此精确地对齐行很容易!

This was hands-down the easiest approach. 这是最简单的方法。 I would have been fighting the damned layout manager all along the way. 我将一直与该死的布局经理进行战斗。 ...DO NOT BE AFRAID TO SKIP A LAYOUT MANAGER AND DO IT YOURSELF, just be prepared to do the whole job yourself. ...不要害怕自己动手做布局管理器,而要自己做,只是要做好自己的全部准备。 If you're up to that task, and if you have a fixed-use-case situation like I had, it's not so bad at all, and it might even be the only practical way for some tasks. 如果您要完成这项任务,并且您有像我这样的固定用例情况,那还不算太糟, 它甚至可能是某些任务的唯一实用方法。

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

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