The 12th Chapter-swing Programming---Special containers in swing--jsplitpane
(i) Use of JSplitPane
JSplitPane is used to create a split board that splits a component (usually a container) into two parts and provides a split bar. The user can drag the divider to resize two parts. The essence of the divider panel is a special container that can hold only two components, And split panel is divided into upper and lower segmentation, left and right split two cases.
The code for creating the split panel is as follows:
New JSplitPane (direction, left/upper component, right/down component);
The JSplitPane split panel provides several ways to do this:
(1) setdividerlocation (double proportionallocation): Sets the position of the separator bar to a percentage of jsplitpane.
(2) setdividerlocation (int location): Sets the position of the separator bar by the pixel value.
(3) setdividersize (int newSize): Sets the size of the separator bar by the pixel value.
(4) Setleftcomponent (Component comp)/settopcomponent (Component comp): Place the specified component to the left or top of the split panel.
(5) Setrightcomponent (Component comp)/setbottomcomponent (Component comp): Place the specified component to the right or bottom of the split panel.
The following procedure demonstrates the use of JSplitPane:
Importjava.awt.*;Importjava.awt.event.*;ImportJava.util.*;Importjavax.swing.*;Importjavax.swing.event.*; Public classsplitpanetest{book[] Books=Newbook[]{NewBook ("Struts2 authoritative guide",NewImageIcon ("Ico/struts2.jpg") , "A comprehensive introduction to the Struts2 of all aspects of knowledge"), NewBook ("Lightweight EE Enterprise Application Practice",NewImageIcon ("Ico/j2ee.jpg") , "Introduction to Struts, spring and \nhibernate integrated development knowledge"), NewBook ("The AJAX Toolkit based on the Java EE",NewImageIcon ("Ico/ajax.jpg") , "Comprehensive introduction to all aspects of ajax\n development on the Java EE platform") }; JFrame JF=NewJFrame ("Test JSplitPane"); JList Booklist=NewJList (books); JLabel bookcover=NewJLabel (); JTextArea Bookdesc=NewJTextArea (); Public voidinit () {//set the optimal size for three componentsBooklist.setpreferredsize (NewDimension (150, 300)); Bookcover.setpreferredsize (NewDimension (300, 150)); Bookdesc.setpreferredsize (NewDimension (300, 150)); //Add an event listener to the drop-down listBooklist.addlistselectionlistener (NewListselectionlistener () { Public voidvaluechanged (Listselectionevent event) { Book Book=(book) Booklist.getselectedvalue (); Bookcover.seticon (Book.getico ()); Bookdesc.settext (Book.getdesc ()); } }); //Create a vertical split panel,//Put bookcover on top, put BOOKDESC below, support continuous layoutJSplitPane left =NewJSplitPane (Jsplitpane.vertical_split,true, Bookcover,NewJScrollPane (BOOKDESC)); //turn on the One Touch show featureLeft.setonetouchexpandable (true); //The following code sets the size of the split bar. //Left.setdividersize (50); //set the split panel to adjust the layout based on the optimal size of the included componentsleft.resettopreferredsizes (); //Create a horizontal split panel//Leave the left component on the right and the Booklist componentJSplitPane content =NewJSplitPane (Jsplitpane.horizontal_split, left, Booklist); Jf.add (content); Jf.setdefaultcloseoperation (Jframe.exit_on_close); Jf.pack (); Jf.setvisible (true); } Public Static voidMain (string[] args) {Newsplitpanetest (). Init (); }}classbook{PrivateString name; PrivateIcon ico; PrivateString desc; PublicBook () {} PublicBook (string name, Icon ico, string desc) { This. Name =name; This. ico =ico; This. desc =desc; } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {return This. Name; } Public voidSetico (Icon ico) { This. ico =ico; } PublicIcon Getico () {return This. ico; } Public voidSetdesc (String desc) { This. desc =desc; } PublicString GetDesc () {return This. Desc; } PublicString toString () {returnname; }}