简体   繁体   English

Java Swing架构问题?

[英]Java Swing architecture problem?

Till now, I have developed simple swing applications so there was no need to break the GUI code into diff. 到目前为止,我已经开发了简单的swing应用程序,因此无需将GUI代码分解为diff。 classes but as this application is going to be very large, I decided to break the code into diff. 类,但由于此应用程序将非常大,因此我决定将代码拆分为diff。 classes so as to make the code more manageable. 类,以使代码更易于管理。

But before proceeding, I have some doubts in my mind which are as follows: 但是在继续之前,我心中有以下疑问:

Brief description of GUI GUI的简要说明

It will have a main JFrame (MainFrame). 它将有一个主JFrame(MainFrame)。 On that a JPanel(MainJPanel) is set whose layout is to be CardLayout. 设置一个JPanel(MainJPanel),其布局将为CardLayout。 It will contain 25 cards (each card is in the form of JPanel which contains its own swing components). 它将包含25张卡(每张卡都是JPanel形式,其中包含自己的挥杆组件)。

Q1. Q1。 I have decided to make 25 classes (each for one JPanel card). 我决定进行25节课(每节课需要一张JPanel卡)。 Is it correct approach? 这是正确的方法吗?

Q2. Q2。 If the above answer is correct, then how can I write code of xxxxActionPerformed() methods of buttons which are on those cards (25 cards) as these methods need access to the object of MainJPanel 如果以上答案正确,那么我该如何编写这些卡(25张卡)上按钮的xxxxActionPerformed()方法的代码,因为这些方法需要访问MainJPanel的对象

eg 例如

public void buttonActionPerformed(ActionEvent evt) {
    java.awt.CardLayout c = (java.awt.CardLayout) mainJPanel.getLayout();
    c.show(mainJPanel, "card1");  // card1 is this card
    mainJPanel.updateUI();
}

I googled for swing examples but almost all of them shows the use of diff. 我用谷歌搜索了swing示例,但是几乎所有示例都显示了diff的用法。 swing components. 摆动组件。 Can you also please suggest me a link that shows some swing examples that contain GUI codes in diff. 您能否也建议我一个链接,该链接显示了一些diff示例中包含GUI代码的示例。 classes. 类。

Q1) That sounds like quite a lot of classes. Q1)听起来很多课程。 While it's possible that each class has distinct functionality I find it more likely that you could combine some of those into more common classes. 虽然每个类都有可能具有不同的功能,但我发现您更有可能将其中一些组合成更常见的类。 For example instead of YellowCard and BlueCard you could simply have ColorCard where color is a parameter. 例如,您可以简单地使用ColorCard(其中Color是参数)来代替YellowCard和BlueCard。

Q2) Model View Presenter (MVP) and Model View Controller (MVC) are two (or one, depending on your view) common design patterns which help design GUIs so that everyone has the data they need. Q2)Model View Presenter(MVP)和Model View Controller(MVC)是两种(或一种,取决于您的视图)常见的设计模式,这些模式可帮助设计GUI,以便每个人都能获得所需的数据。

More specifically, you might not need all cards to have a reference to the parent panel. 更具体地说,您可能不需要所有卡都具有对父面板的引用。 For example, if you have a BurgerPanel which allows the user to order burgers and a StatusPanel which shows how many burgers have been ordered you can communicate between them as follows... 例如,如果您有一个允许用户订购汉堡的BurgerPanel和一个显示已订购多少汉堡的StatusPanel,则可以按以下方式在它们之间进行通信...

Create a StoreStatus object and pass it to both BurgerPanel and StatusPanel. 创建一个StoreStatus对象,并将其传递给BurgerPanel和StatusPanel。 When the user orders a burger with burger panel it updates the store status. 当用户订购带有汉堡面板的汉堡时,它将更新商店状态。 The store status notifies the StatusPanel of this update via the observer pattern and then the StatusPanel reflects the change. 存储状态通过观察者模式将此更新通知StatusPanel,然后StatusPanel反映更改。

UPDATE: In regards to your specific example you would either some kind of reference to the parent class or you could notify it of updates with the observer patterns. 更新:关于您的特定示例,您可以对父类进行某种引用,也可以使用观察者模式将其更新通知给它。 (The advantage of the observer pattern is that any changes to the parent class couldn't create changes in the child classes.) (观察者模式的优点是,对父类的任何更改都不能在子类中创建更改。)

I would say you are correct in creating a class for each card. 我会说您为每张卡创建一个类都是正确的。 This is a logical way to split up the code. 这是拆分代码的逻辑方法。

If you need to reference the MainJPanel then simply pass it into the constructor of each card class and keep a reference to it. 如果您需要引用MainJPanel,则只需将其传递到每个卡类的构造函数中并保留对其的引用。

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

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