[英]How can I make multiple instances of the same panel and change attributes in each one individually?
Is it possible to make a Jpanel, put variables inside of it, and then add the same panel to the content pane multiples times( the number of times based on the number of a certain variable outside of the GUI). 是否可以制作一个Jpanel,将变量放入其中,然后将同一面板多次添加到内容窗格中(该次数基于GUI外部某个变量的数量)。 AND THEN edit the content inside of each instance individually?
然后分别编辑每个实例内部的内容?
Here is why I need to do the things mentioned above: I'm writing a program that several different clinics can use with minimum setup on the user side. 这就是为什么我需要做上面提到的事情的原因:我正在编写一个程序,供几个不同的诊所使用,而用户端的设置最少。 They specify how many bedrooms they have and the my program creates a visual representation of each bedroom automatically.
他们指定他们有多少间卧室,而我的程序会自动创建每间卧室的视觉表示。 For example: a clinic has 13 rooms so my program adds 13 bedroom Jpanels to the content pane and displays them in numerical order using a fullscreen view.
例如:一家诊所有13个房间,因此我的程序将13个卧室的Jpanels添加到内容窗格中,并使用全屏视图按数字顺序显示它们。
No, you can't do this. 不,你不能这样做。 Each
JComponent
(which includes JPanel
can have one and only one parent). 每个
JComponent
(包括JPanel
都可以只有一个父对象)。
You could "fake" it by using a JTable
and having your panel as the cell renderer for the table, that only works if you have no interactive controls inside the panel though. 您可以通过使用
JTable
并将面板用作表格的单元格渲染器来“伪造”它,但这仅在面板中没有交互控件的情况下才有效。
What you need to do is either create a factory that generates these JPanel
s for you as you need them and just create the number you need or create a new subclass of JPanel
that has everything set up as you like and add new instances of that. 您需要做的是创建一个工厂,以便根据需要为您生成这些
JPanel
,然后仅创建所需的编号,或者创建一个JPanel
的新子类,该子类可以根据需要进行设置并添加新的实例。
我对Swing和Java还是陌生的,但是我想您可以使用扩展JPanel的所有变量创建一个单独的类,然后创建一个上述类的ArrayList,然后再添加.client但要添加许多客户端。但是,如果没有某种识别方法,可能无法单独进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.