java swing CardLayout中每张卡的单独类 [英] A individual class for each Card in java swing CardLayout

查看:107
本文介绍了java swing CardLayout中每张卡的单独类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于架构和设计的目的,我想为Java Swing CardLayout中的每张卡设计一个类。然后有一个构建GUI的mainapp。我现在无法做到这一点。

For architecture and design purposes I would like to design my GUI with a class for each card in a Java Swing CardLayout. and then have a mainapp that builds the GUI. I am having trouble doing this right now.

我想举例说明主菜单的所有按钮位置等等,然后只是实例化该卡并将其添加到另一个类的布局中。有谁知道如何实现这个目标?

I would like to example have a class for the main menu with all the button locations etc. and then just instantiate that card and add it to the layout in another class. Does anyone know how to achieve this?

推荐答案

也许你想给你的类使用CardLayout一个公共的loadCard方法,喜欢

Perhaps you want to give your class that uses the CardLayout a public loadCard method, something like

public void loadCard(JComponent component, String key) {
  cardHolderPanel.add(component, key);
}

其中cardHolderPanel是持有卡片的容器。

where cardHolderPanel is the container that holds the cards.

由于您创建的类充当卡片,因此请考虑将它们全部扩展为基本抽象类或具有允许此类保存其自己的键的方法的接口串。或者只是使用JComponent name属性让组件拥有自己的键String,可以通过 getName()轻松获得。

Since your creating classes to act as cards, consider having them all extend from a base abstract class or an interface that has a method that allows this class to hold its own key String. Either that or simply use the JComponent name property to have a component hold its own key String, one that can easily be obtained via getName().

有关更详细的答案,您可能需要向我们提供有关您当前申请及其结构的更多详细信息。

For a more detailed answer, you may need to give us more details on your current application and its structure.

这篇关于java swing CardLayout中每张卡的单独类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆