thinhare
[GUI programming] How do two JPanels interact?
Hi, all
I've been looking if there is a standard/mostly-used way implementing GUI applications. e.g.,
under a JFrame, there are 2 MyPanel exntending JPanel, one placed on the leftside, the other on the rightside. they both have got a bunch of JButtons, JLabels, JList, etc. this is the view.
but for the control part, here comes the question: I'd would like PanelA to change background color if a Button on PanelB is pressed. where should I place this part of code that controls what happen. I've considered some possibilites, but not sure which one is the best.
1. implement it in main() like: if PanelA.Button.isPressed(), then PanelB.setBackground(Green)
pros: centrailized control
cons: could lead to long & messy code if there are many Panels needed to control
2. PanelA as Observer, PanelB Observable
pros: doable
cons: not very convenient
3. PanelA & PanelB both singleton, so they can reference to each other directly
pros: easy implementing
cons: less flexibility
if none of them is the way you do, how do you do it then?
any help will be appreciated.
thanks.
Jae_Cheung
你好,我想你需要的是一种简单的、灵活的且能够重用的方式去开发一个 GUI 应用。我不想给出你这个例子的代码了,因为没时间。但我推荐你看一篇文章 [url=http://www.ibm.com/developerworks/cn/views/java/tutorials.jsp?cv_doc_id=110085][color=Blue]http://www.ibm.com/developerworks/cn/views/java/tutorials.jsp?cv_doc_id=110085[/color][/url] 这是 IBM developerworks 上的一篇关于 Spring 开发 Swing GUI 的简单教程,你需要注册一个 IBM 帐号才能浏览。其中用到了 Spring 的[i]依赖注入(dependency injection),你可能需要学习一下相关知识。看完此教程,我想你应该知道如何开发一个良好的客户端程序。对于该教程中例子,如果你不想使用 Spring 的话,可以自己写一个管理类来替代 xml 配置文件,关键是编程的思想。最后建议你去熟悉一下 Spring RCP 和 Eclipse RCP 两个框架,如果你真的要做 GUI 的话。Good lucks!
[[i] 本帖最后由 Jae_Cheung 于 2008-6-26 01:44 编辑 [/i]]