简体   繁体   English

Java GUI:文档对象模型

[英]Java GUI: Document Object Model

HTML has a Document Object Model, which Javascript can then manipulate / move around. HTML有一个文档对象模型,然后Javascript可以操作/移动。

When I create GUIs in Swing -- the model appears to be very difference (I don't know the name of the model), since I'm creating layout managers, and sticking objects inside of them. 当我在Swing中创建GUI时 - 模型看起来非常不同(我不知道模型的名称),因为我正在创建布局管理器,并在其中粘贴对象。

My question: is there someway to manipulate Java GUis in a DOM like manner? 我的问题:是否有某种方式以类似DOM的方式操纵Java GUis?

[For example, I want to be able to delete / add nodes, move childs around, etc ...] [例如,我希望能够删除/添加节点,移动孩子等...]

Thanks! 谢谢!

For Swing components, everything starts from a set of JFrame's (you can also have JWindow's and JDialog's, but you usually have at least one root frame). 对于Swing组件,一切都从一组JFrame开始(你也可以有JWindow和JDialog,但你通常至少有一个根框架)。 Most likely, all you care about is the contentPane of that JFrame (but you could care also about its ownedWindows, etc...). 最有可能的是,你所关心的只是那个JFrame的contentPane(但你也可以关心它拥有的Windows等等)。

So from the JFrame, you can get the content pane as following: 因此,从JFrame中,您可以获取内容窗格,如下所示:

Container contentPane = frame.getContentPane();

From there, you can start going down the Tree of components, using: 从那里,您可以开始使用以下组件树:

Component[] children = contentPane.getComponents();

From a child, you can get its parent with: 从孩子那里,你可以得到它的父母:

Container parent = child.getParent();

To add a component to a container: 要将组件添加到容器:

container.add(someComponent);
container.validate();

To remove a component from a container: 要从容器中删除组件:

container.remove(someComponent);
container.validate();

To move a component from one Container to another, simply remove it from one and add it to the other. 要将组件从一个Container移动到另一个Container,只需将其从一个Container中删除并将其添加到另一个Container即可。

I am not sure this answers your question. 我不确定这会回答你的问题。 It would be easier if you could post real examples of what you are trying to do. 如果你能发布你想要做的事情的真实例子会更容易。

我不确定这是否解决了您的问题,但有XML驱动的Java UI工具包

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

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