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