[英]What is the best way to add components to a java application
Right now I create all of my swing components in the same class that has my method "main", this class keeps growing and looking more unorganized with each component created. 现在,我在具有我的方法“ main”的同一个类中创建所有的swing组件,该类不断增长,并且每个创建的组件看起来更加杂乱无章。 Should I create another class to create the components and pass a the frame by reference?
我应该创建另一个类来创建组件并通过引用传递框架吗? Or should I let my current class keep growing?
还是应该让我目前的课程保持增长? Is there a best practice or industry standard?
是否有最佳做法或行业标准?
Thank you for any input. 感谢您的投入。
The short answer is "it depends." 简短的答案是“取决于情况”。 If it is a simple UI, you can get away with one class.
如果它是一个简单的UI,那么您可以不用上一堂课。 But if there are several independent windows or other components, consider creating separate classes.
但是,如果有几个独立的窗口或其他组件,请考虑创建单独的类。 Then instantiate them in your main class.
然后在您的主类中实例化它们。
For example, consider a banking application. 例如,考虑一个银行应用程序。 There may be different windows for the different functions (withdraw, deposit, transfer, balance inquiry).
不同功能(取款,入金,转账,余额查询)的窗口可能不同。 You could have four different classes that extend JPanel.
您可以有四个不同的类来扩展JPanel。 Then in your main class, you can instantiate these classes and assemble them.
然后,在您的主类中,您可以实例化这些类并进行组装。
Just a thought. 只是一个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.