Java GUI:文档对象模型 [英] Java GUI: Document Object Model

查看:120
本文介绍了Java GUI:文档对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我在Swing中创建GUI时,模型似乎是非常不同的(我不知道模型的名称),因为我正在创建布局管理器,并在其中粘贴对象。



我的问题:有没有可以操纵Java GUIS以类似DOM的方式?



[例如,我想删除/添加节点,移动小孩等...]



谢谢!

解决方案

对于Swing组件,一切都从一组JFrame的(您也可以拥有JWindow和JDialog,但通常至少有一个根框架)。很可能,你所关心的只是JFrame的contentPane(但是你也可以关心它所拥有的Windows等等)。



所以从JFrame,你可以得到如下内容窗格:

  Container contentPane = frame.getContentPane(); 

从那里,您可以使用以下方式开始下载组件树:

  Component [] children = contentPane.getComponents(); 

从一个孩子,你可以得到它的父母:

  Container parent = child.getParent(); 

要向容器添加组件:

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

从容器中删除组件:

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

要将组件从一个容器移动到另一个容器,只需将其从一个容器中删除并将其添加到另一个



我不知道这回答了你的问题。如果你可以发表你想要做的事情的真实例子会更容易。


HTML has a Document Object Model, which Javascript can then manipulate / move around.

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.

My question: is there someway to manipulate Java GUis in a DOM like manner?

[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). Most likely, all you care about is the contentPane of that JFrame (but you could care also about its ownedWindows, etc...).

So from the JFrame, you can get the content pane as following:

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.

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.

这篇关于Java GUI:文档对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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