[JAVA100 example]022, split a panel

Source: Internet
Author: User
Tags split

Import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


import javax.swing.event.*;


import java.util.*;





/**


* <p>title: Split panel </p>


* <p>description: Demo split panel into left and right parts </p>


* <p>copyright:copyright (c) 2003</p>


* <p>filename:splitpanedemo.java </p>


* @version 1.0


*/


public class Splitpanedemo implements Listselectionlistener {


private string[] imagenames={"Bird.gif", "Cat.gif", "Dog.gif", "Pig.gif"};


private JLabel picture;


private JList list;


private JSplitPane Splitpane;


/**


*<br> Method Description: The constructor, which defines all the artifacts to use


*<br> input Parameters:


*<br> return type:


*/


public Splitpanedemo () {


   


//Create a list of image names, set as a single option


list = new JList (imagenames);


List.setselectionmode (listselectionmodel.single_selection);


list.setselectedindex (0);


List.addlistselectionlistener (this);


JScrollPane Listscrollpane = new JScrollPane (list);





//Get the default picture


ImageIcon firstimage = Createimageicon ("images/" +


(String) imagenames[0]);


if (firstimage!= null) {


picture = new JLabel (firstimage);


picture.setpreferredsize (New Dimension (firstimage.geticonwidth),


firstimage.geticonheight ());


} else {


picture = new JLabel ((String) imagenames[0]);


    }


JScrollPane Picturescrollpane = new JScrollPane (picture);





//Create a horizontal split panel that defines the names of two panels.


Splitpane = new JSplitPane (jsplitpane.horizontal_split,true,


Listscrollpane, Picturescrollpane);


splitpane.setonetouchexpandable (TRUE);


splitpane.setdividerlocation (150);





//define the minimum size of the panel


Dimension minimumsize = new Dimension (100, 50);


listscrollpane.setminimumsize (minimumsize);


picturescrollpane.setminimumsize (minimumsize);





//define Initial size


splitpane.setpreferredsize (New Dimension (400, 200));


  }


/**


*<br> Method Description: Get this split panel


*<br> input Parameters:


*<br> return Type: JSplitPane object


*/


public JSplitPane Getsplitpane () {


return splitpane;


  }


/**


*<br> Method Description: List Listener event handling


*<br> Input parameters: listselectionevent E list select event


*<br> return type:


*/


public void valuechanged (Listselectionevent e) {


if (e.getvalueisadjusting ())


return;





JList thelist = (JList) e.getsource ();


if (Thelist.isselectionempty ()) {


Picture.seticon (NULL);


picture.settext (NULL);


} else {


int index = Thelist.getselectedindex ();


ImageIcon newimage = Createimageicon ("images/" +


(String) imagenames[index]);


Picture.seticon (newimage);


if (newimage!= null) {


picture.settext (NULL);


picture.setpreferredsize (New Dimension (newimage.geticonwidth),


newimage.geticonheight ());


else {


Picture.settext ("Image not Found:"


+ (String) imagenames[index]);


      }


picture.revalidate ();


    }


  }


/**


*<br> Method Description: Get the drawing object based on the path


*<br> input parameter: String path picture Path


*<br> return type: ImageIcon Picture Object


*/


protected static ImageIcon Createimageicon (String path) {


Java.net.URL imgurl = SplitPaneDemo.class.getResource (path);


if (imgurl!= null) {


return new ImageIcon (Imgurl);


} else {


System.err.println ("couldn´t Find File:" + path);


return null;


    }


  }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


jframe.setdefaultlookandfeeldecorated (TRUE);





//define form


JFrame frame = new JFrame ("Splitpanedemo");


frame.setdefaultcloseoperation (jframe.exit_on_close);


Splitpanedemo Splitpanedemo = new Splitpanedemo ();


Frame.getcontentpane (). Add (Splitpanedemo.getsplitpane ());





//Display form


Frame.pack ();


frame.setvisible (TRUE);


  }


}

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.