import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.util.*;

public class SplitPaneDemo implements ListSelectionListener {

private Vector imageNames;

private JLabel picture;

private JList list;

private JSplitPane splitPane;

public SplitPaneDemo() {

//从 properties 文件中读图片名字

ResourceBundle imageResource;

try {

imageResource = ResourceBundle.getBundle("imagenames");

String imageNamesString = imageResource.getString("images");

imageNames = parseList(imageNamesString);

} catch (MissingResourceException e) {

handleMissingResource(e);

}

//创建一个列表并把它放到滚条面板上

list = new JList(imageNames);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list.setSelectedIndex(0);

list.addListSelectionListener(this);

JScrollPane listScrollPane = new JScrollPane(list);

//设置图片标签，并把它放到滚条面板上

ImageIcon firstImage = createImageIcon("images/" +

(String)imageNames.firstElement());

if (firstImage != null) {

picture = new JLabel(firstImage);

} else {

picture = new JLabel((String)imageNames.firstElement());

}

JScrollPane pictureScrollPane = new JScrollPane(picture);

//创建一个切分面板（split pane），把两个滚条面板放入里面

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,

listScrollPane, pictureScrollPane);

splitPane.setOneTouchExpandable(true);

splitPane.setDividerLocation(150);

//设置切分面板里两个滚条面板的最小尺寸

Dimension minimumSize = new Dimension(100, 50);

listScrollPane.setMinimumSize(minimumSize);

pictureScrollPane.setMinimumSize(minimumSize);

//设置切分面板的大小

splitPane.setPreferredSize(new Dimension(400, 200));

}

//Used by SplitPaneDemo2

public JList getImageList() {

return list;

}

public JSplitPane getSplitPane() {

return splitPane;

}

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.elementAt(index));

picture.setIcon(newImage);

if (newImage != null) {

picture.setText(null);

} else {

picture.setText("图片没有找到: "

+ (String)imageNames.elementAt(index));

}

}

}

protected static Vector parseList(String theStringList) {

Vector<String> v = new Vector<String>(10);

StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");

while (tokenizer.hasMoreTokens()) {

String image = tokenizer.nextToken();

v.addElement(image);

}

return v;

}

/** 当图片 property 文档找不到时调用. */

private void handleMissingResource(MissingResourceException e) {

System.err.println();

System.err.println("找不到包含图片文件名字的 properties 文件");

System.err.println("这个文件名应该是 imagenames.properties, ");

System.err.println("并且应该包含单一的行。这一行写明了一个或多个可以在 images 目录下找到的图片文件。比如：");

System.err.println();

System.err.println(" images=Bird.gif Cat.gif Dog.gif");

System.err.println();

throw(e); //通常 1 退出 exit(1), 由控制台处理错误信息。

}

/** 返回一个图标，如果路径不对的话则为 null */

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("找不到文件: " + path);

return null;

}

}

/**

* 创建并显示这个 GUI，由事件分配线程来处理。

*/

private static void createAndShowGUI() {

//创建并设置窗口

JFrame frame = new JFrame("SplitPaneDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SplitPaneDemo splitPaneDemo = new SplitPaneDemo();

frame.getContentPane().add(splitPaneDemo.getSplitPane());

//显示窗口

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//给事件分配线程一个作业：创建并显示这个 GUI

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}