简体   繁体   中英

How to get a copy of a JComponent?

I have a javax.swing.JButton which I have created to sign out. How can I get an exact copy(duplicate) of the button to another JFrame ?

private JButton signOut;    

public JButton getBtn(){
    //Any idea?
}

return signOut; is not an option, because then it will screw the home page when I call setBounds() or setVisible() of signOut .

Do I have to create a new Object of the class with the button to get only this component? Or is there another solution?

Abstract the building of that button into some sort of factory or function. This way, you don't have to constantly manually reconfigure the button nor do you have to worry about copying / cloning (which can get very messy)

public JButton createButton(string caption) {
   //Create button here
}

Ok then, you can copy that button and paste in another panel. Go back to the old frame. Copy that button code. Just come back to the next frame. Click on the button action and paste it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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