繁体   English   中英

我如何在Java中创建一个具有另一个属性而不声明它的新对象?

[英]How do I create a new object in java that has the properties of another one without declaring it?

例如,我有一个带有设置图标,名称和标签的JButton; 如何制作可以添加到面板的新JButton?

JButton b1=new JButton();
b1.set[whatever needs to be set];
JPanel p=new JPanel();
p.add(new JButton()[that has the properties of b1]);

使用完全相同的属性? 然后,您可以尝试b1.clone() 您的情况将是p.add(b1.clone()); clone()可以创建对象的浅表副本,请参见clone() 并检查此答案以进行复制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM