简体   繁体   English

Java:如何根据选择项目菜单或JTree节点更改JPanel的内容?

[英]Java: How to change content of JPanel according to choice a item menu or JTree node?

I'm a web developer and I'm new in Java and I want know how to change content of a JPanel when choice any node of JTree or any item of menu. 我是一名Web开发人员,我是Java新手,我想知道在选择JTree的任何节点或任何菜单项时如何更改JPanel的内容。 Because I don't want display many windows when click in any option but will reload only part of the program as if it were an ajax call in Web. 因为我不希望在单击任何选项时显示许多窗口,但只会重新加载程序的一部分,就好像它是Web中的ajax调用一样。

IE I want to display different content in the JPanel according to the option chosen, but now how. IE我想根据所选的选项在JPanel中显示不同的内容,但现在如何。

If you could give me some tips, items to use, a better option to do or some example, greatly appreciate. 如果你可以给我一些提示,使用的项目,更好的选择或一些例子,非常感谢。

If you're a web developer, then it should be simple, because it works in a similar way as in JavaScript: you need to register an event listener on the tree ( TreeSelectionListener ) and on the menu item ( ActionListener ). 如果您是Web开发人员,那么它应该很简单,因为它的工作方式与JavaScript类似:您需要在树( TreeSelectionListener )和菜单项( ActionListener )上注册事件侦听器。 When the user selects a node or chooses the menu item, the event you're interested in (node selected, or action performed on the menu item) will be fired, your listener will be called, and this listener just needs to replace the panel content with the new one. 当用户选择节点或选择菜单项时,您将感兴趣的事件(选择节点或对菜单项执行的操作)将被触发,您的侦听器将被调用,并且此侦听器只需要替换面板内容与新的。

Read the tutorial about JTree and the tutorial about JMenu for more guidance. 阅读有关JTree教程有关JMenu教程以获得更多指导。

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

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