我想将JPanel
从<{1}} A 克隆到JFrame
B 。
我研究了有关深度克隆的http://www.agiledeveloper.com/articles/cloning072002.htm并遵循了指南。
但JFrame
克隆JFrame
后,add()
B 仍为空白
后来我在Duplicating a JPanel - (Copy JPanel's static image to different JPanel)找到了一个类似的问题,但答案并不是我想要的,因为我的jpanel涉及用户互动,这不应该只是一个图像。
任何人都可以给我一个提示吗?
JPanel
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JframeA extends JFrame {
private JButton addToSecondFrame;
private JframeB secondFrame = new JframeB();
;
private SomeJPanelClass myJPanelOri = new SomeJPanelClass();
private SomeJPanelClass myJPanelClone;
public JframeA() {
setTitle("cloning JPanel");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void run() {
addToSecondFrame = new JButton("Add to Second Frame");
addToSecondFrame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myJPanelClone = (SomeJPanelClass) myJPanelOri.clone();
secondFrame.getContentPane().add(myJPanelClone);
secondFrame.validate();
secondFrame.setVisible(true);
}
});
this.add(myJPanelOri);
this.add(addToSecondFrame);
}
public static void main(String[] args) {
JframeA test = new JframeA();
test.run();
}
}
import javax.swing.JFrame;
public class JframeB extends JFrame {
private SomeJPanelClass copiedJPanel;
public JframeB() {
setTitle("Frame B");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
public class SomeCanvasClass extends Canvas implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener, Cloneable {
public SomeCanvasClass() {
setBackground(Color.red);
setSize(500, 500);
}
@Override
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
编辑:添加了源代码