Java implementation Click on a jlabel to add a jlabel small function

Source: Internet
Author: User

When the interface is generated, I want to do a click on a JLabel add a jlabel, that is, similar to the QQ add friends can be added to a friend in a timely manner. Oneself to do for a long time, found not timely refresh. I looked it up on the internet and studied it for a little while. Discover the need to validate this function to re-layout. Wrote a little feature of this.

 PackageQq.client.tools;Importjava.awt.event.MouseEvent;ImportJava.awt.event.MouseListener;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel; @SuppressWarnings ("Serial")) Public classButtonframeextendsjframe{ PublicButtonframe () {//constructor FunctionButtonpanel =NewJPanel ();//Create a JPanel       This. setSize (200, 500);//set the size of the windowMakebutton ();//calling Functions               This. Add (Buttonpanel);}  Public voidMakebutton () {JLabel Buttonfather=NewJLabel ("I ' m father");//Create a buttonButtonfather.addmouselistener (NewMouseListener () { Public voidmousereleased (MouseEvent e) {} @Override Public voidmousepressed (MouseEvent e) {} Public voidmouseexited (MouseEvent e) {} Public voidmouseentered (MouseEvent e) {} Public voidmouseclicked (MouseEvent e) {AddButton (e);    }}); Buttonpanel.add (Buttonfather); }  Private voidAddButton (MouseEvent e) {JLabel Buttonson=NewJLabel ("I ' m Son");//Create a labelButtonpanel.add (Buttonson);//Add to Panel   This. Validate ();} PrivateJPanel Buttonpanel; Public Static voidMain (string[] args) {NewButtonframe (). setvisible (true); }}

As with deleting JLabel, there is no need to use that function to refresh the interface to add JLabel.

Java implementation Click on a jlabel to add a jlabel small function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.