简体   繁体   English

如何制作同一面板的多个实例并分别更改每个面板的属性?

[英]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.

相关问题 如何防止对象的多个实例在Java中具有相同的属性 - How can I prevent multiple instances of an object from having the same attributes in Java 如何制作与面板相同尺寸的图像? - How can I make an image the same size as the panel? 如何使用相同的面板更新面板? - How I can update a panel with the same panel? 如何使用Matcher分别替换每个组? - How can I replace each group individually using Matcher? 如何在一条语句中打印多个类实例? 爪哇 - How can I print multiple class instances in one statement? Java 如何在同一面板Extjs 4.2中多次使用同一xtype? - How can i use same xtype multiple time within same panel Extjs 4.2? 如何让 Swing 中的多个按钮单独工作? - How to make multiple buttons work individually in Swing? 如何在Java中更改面板的大小? - How can I change the size of a Panel in Java? 如何更改单击的按钮的颜色,但使其只能同时更改一个按钮的颜色? - How can I change the color of a button that is clicked but make it so that only one buttons' color can be changed at the same time? 如何在多标签窗格的每个标签中放置退出按钮,以分别关闭每个按钮 - How do I put an exit button in every tab of a multi tabbed pane that will close each one individually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM