A recent need to complete a demand: with swing a tree-like menu, with a Level two menu, click the First menu to expand the Level two menu, and the number of two-level menu more than the Preview view will appear scroll bar. Because of the lack of swing research, it took a lot of energy!
First look at the test:
To close a picture:
Expand Diagram:
Full Source:
1 PackageCom.xuwei.test2;2 3 Importjava.awt.BorderLayout;4 ImportJava.awt.Color;5 Importjava.awt.GridLayout;6 Importjava.awt.event.ActionEvent;7 ImportJava.awt.event.ActionListener;8 9 ImportJavax.swing.JButton;Ten ImportJavax.swing.JFrame; One ImportJavax.swing.JPanel; A ImportJavax.swing.JScrollPane; - - the Public classTestFrm4extendsjframe{ - PrivateJButton btn1,btn2,btn3,btn4,btn5; - PrivateJPanel Pnorth,psouth,submenucontainer; - PrivateJScrollPane Pcenter; + Privatejbutton[] Btn =NULL; - Private Static BooleanExpand=false; + A PublicTestFrm4 () { atbtn1=NewJButton ("Grade1 menu1"); - Btn1.setbackground (Color.cyan); -Btn2=NewJButton ("Grade1 menu2"); - Btn2.setbackground (Color.cyan); -btn3=NewJButton ("Grade1 menu3"); - Btn3.setbackground (Color.cyan); inBtn3.addactionlistener (NewActionhandler ()); - tobtn4=NewJButton ("Grade1 menu4"); + Btn4.setbackground (Color.cyan); -btn5=NewJButton ("Grade1 menu5"); the Btn5.setbackground (Color.cyan); *Pnorth=NewJPanel (); $Pnorth.setlayout (NewGridLayout (3,1));Panax NotoginsengPsouth=NewJPanel (); -Psouth.setlayout (NewGridLayout (2,1)); theSubmenucontainer=NewJPanel (); +Submenucontainer.setlayout (NewGridLayout (25,1)); A thebtn=NewJbutton[25]; + for(inti=0;i<btn.length;i++){ -btn[i]=NewJButton ("[Menu" +i+ "]"); $ Btn[i].setbackground (color.white); $ } - - This. setlayout (NewBorderLayout ()); the - Pnorth.add (BTN1); Pnorth.add (BTN2); Pnorth.add (BTN3) ;Wuyi for(inti=0;i<btn.length;i++){ the Submenucontainer.add (Btn[i]); - } WuPcenter=NewJScrollPane (submenucontainer); - About Psouth.add (BTN4);p south.add (BTN5); $ This. Add (Pnorth, "North"); - This. Add (Pcenter, "Center"); - This. Add (Psouth, "South"); - A This. setvisible (true); + This. SetSize (500,600); the This. setresizable (false); - This. Setlocationrelativeto (NULL); $ This. Setdefaultcloseoperation (jframe.exit_on_close); the the } the the - in Private classActionhandlerImplementsactionlistener{ the the @Override About Public voidactionperformed (ActionEvent e) { the if(btn3==E.getsource ()) { the if(expand) {//Folding thePnorth.setlayout (NewGridLayout (3,1)); + Pnorth.remove (BTN4);p north.remove (BTN5); - Psouth.add (BTN4);p south.add (BTN5); the for(inti=0;i<btn.length;i++){Bayi Submenucontainer.add (Btn[i]); the } the validate (); - Getcontentpane (). repaint (); -Expand=false; the}Else{//Expand the for(inti=0;i<btn.length;i++){ the Submenucontainer.remove (Btn[i]); the } - Psouth.removeall (); thePnorth.setlayout (NewGridLayout (5,1)); the Pnorth.add (BTN4); the Pnorth.add (BTN5);94 pnorth.repaint (); the pcenter.repaint (); the psouth.repaint (); the validate ();98 Getcontentpane (). repaint (); AboutExpand=true; - }101 }102 }103 104 } the 106 Public Static voidMain (string[] args) {107 108 NewTestFrm4 ();109 } the 111}
There are several ways that swing can be called repeatedly to remove components that need to refresh the interface in a timely manner:
Repaint (), validate (), invalidate (), Dolayout ().
Before I did not call validate () caused the interface refresh a lot of problems!
Swing has to do a lot of research and find things!
Java Swing programming Imitation js tree Folding Menu