任何人都可以建议替代这些类似的JPanel构造函数吗?(Can anyone suggest an alternative to these similar JPanel constructors?)

编程入门 行业动态 更新时间:2024-10-28 04:19:39
任何人都可以建议替代这些类似的JPanel构造函数吗?(Can anyone suggest an alternative to these similar JPanel constructors?)

我有一个包含许多面板的主JFrame 。 在这个JFrame中,每个面板都需要能够访问的方法和变量,所以我一直将框架本身传递给所有这些框架的构造函数。

pnlLogin = new LoginPanel(this); pnlMainMenu = new MainMenuPanel(this); pnlSalesMenu = new SalesMenuPanel(this); pnlPaymentMenu = new PaymentMenuPanel(this); pnlAddCash = new AddCashPanel(this); pnlAddCredit = new AddCreditPanel(this); pnlAddCheck = new AddCheckPanel(this); pnlSaleComplete = new SaleCompletePanel(this); pnlProductMenu = new ProductMenuPanel(this); pnlAddProd = new AddProdPanel(this); pnlUpdateProd = new UpdateProdPanel(this); pnlEmployeeMenu = new EmployeeMenuPanel(this); pnlAddEmp = new AddEmpPanel(this); pnlUpdateEmp = new UpdateEmpPanel(this);

然后在这样的面板中引用JFrame变量和方法

frmMain.frameMethod(); frmMain.frameVariable = x;

我意识到静态方法和变量可以完成这样的事情并允许我在面板中引用它们,但这不适合我的应用程序需求。 谁能建议更好的方法呢?

I have a main JFrame that contains many panels. In this JFrame there are methods and variables that every panel needs to be able to access, so I have been passing the frame itself into the constructors of all of them like this.

pnlLogin = new LoginPanel(this); pnlMainMenu = new MainMenuPanel(this); pnlSalesMenu = new SalesMenuPanel(this); pnlPaymentMenu = new PaymentMenuPanel(this); pnlAddCash = new AddCashPanel(this); pnlAddCredit = new AddCreditPanel(this); pnlAddCheck = new AddCheckPanel(this); pnlSaleComplete = new SaleCompletePanel(this); pnlProductMenu = new ProductMenuPanel(this); pnlAddProd = new AddProdPanel(this); pnlUpdateProd = new UpdateProdPanel(this); pnlEmployeeMenu = new EmployeeMenuPanel(this); pnlAddEmp = new AddEmpPanel(this); pnlUpdateEmp = new UpdateEmpPanel(this);

Then referring to the JFrame variables and methods from within the panels like this

frmMain.frameMethod(); frmMain.frameVariable = x;

I realize that static methods and variables could accomplish something like this and allow me to refer to them in the panels, but that is just not suitable for my applications needs. Can anyone suggest a better way of doing this?

最满意答案

你不应该使用静态。 虽然,Swing对代码非常糟糕。 最佳实践也更加冗长,因为您应该对所有组件使用单例模式,以便它们可以延迟加载(最佳性能)。 例如 :

private JPanel myPanel; public JPanel getMyPanel() { if (myPanel == null) { myPanel = new JPanel(this); } return myPanel; }

这样,您可以在需要时销毁面板,并确保在需要时可以使用新面板。 此外,此组件仅在您需要时加载,而不是之前(最佳内存管理)。

所以,要真正回答你的问题,你没有太多方法可以做得更好。

最后一件事:确保使用getter和setter来访问其他类变量。

frmMain.frameVariable = x;

应该

frmMain.setFrameVariable(x);

You're right not to use static for that. Though, Swing is pretty awful to code. Best practices are also even more verbose, as you should use singleton pattern for all of your components, so that they're lazily-loaded (best perfs). For example :

private JPanel myPanel; public JPanel getMyPanel() { if (myPanel == null) { myPanel = new JPanel(this); } return myPanel; }

This way, you can destroy your panel when you want ot, and be sure you'll get a new one whenever you need it. Also, this component will only be loaded when you need it, and not before (best memory management).

So, to really answer your question, you have not much ways to do it better.

On last thing : be sure to use getter and setter to access other classes variables.

frmMain.frameVariable = x;

should be

frmMain.setFrameVariable(x);

更多推荐

本文发布于:2023-07-19 21:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   任何人都可以   类似   建议   constructors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!