zoukankan      html  css  js  c++  java
  • JSplitPane类测试实例

    package swing;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;

    /**
     *JSplitPane 用于分隔两个(只能两个)Component。
     *本程序是JSplitPane的一个测试例子
     * @author oucq
     *
     */
    public class SplitPaneDemo extends JPanel
                              implements ListSelectionListener {
     
        private JLabel picture;
        private JList list;
        private JSplitPane splitPane;
        //图片名称信息
        private String[] imageNames = { "Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed",
            "kathyCosmo", "lainesTongue", "left", "middle", "right", "stickerface"};
       
       
        public SplitPaneDemo() {

            //创建一个图片列表,放到滚动面板中     
            list = new JList(imageNames);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setSelectedIndex(0);
            list.addListSelectionListener(this);
           
           //创建一个显示指定组件内容的 JScrollPane,只要组件的内容超过视图大小就会显示水平和垂直滚动条。
            JScrollPane listScrollPane = new JScrollPane(list);
           
            picture = new JLabel();
            /*
             * deriveFont:通过复制当前的 Font 对象,并应用新的样式,创建一个新 Font 对象。
             * Font.ITALIC:新 Font 的样式
             */
            picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
            //设置标签内容沿 X 轴的对齐方式。
            picture.setHorizontalAlignment(JLabel.CENTER);
           
            JScrollPane pictureScrollPane = new JScrollPane(picture);

            //创建一个水平方向(沿 x 轴分割) 的JSplitPane对象,用于分割上面创建的两个滚动面板
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                       listScrollPane, pictureScrollPane);
            //设置 oneTouchExpandable 属性的值,要使 JSplitPane 在分隔条上提供一个 UI 小部件来快速展开/折叠分隔条,此属性必须为 true
            splitPane.setOneTouchExpandable(true);
            //设置分隔条的位置
            splitPane.setDividerLocation(150);

            //Dimension 类封装单个对象中组件的宽度和高度(精确到整数)。该类与组件的某个属性关联。由 Component 类和 LayoutManager
            //Provide minimum sizes for the two SplitPaneDemo in the split pane.
            Dimension minimumSize = new Dimension(100, 50);
            //将此组件的最低大小设置为一个常量值
            listScrollPane.setMinimumSize(minimumSize);
            pictureScrollPane.setMinimumSize(minimumSize);

            //Provide a preferred size for the split pane.
            splitPane.setPreferredSize(new Dimension(400, 200));
           
            //显示选中图片
            updateLabel(imageNames[list.getSelectedIndex()]);
        }
       
        //监听列表变化Listens to the list
        public void valueChanged(ListSelectionEvent e) {
            JList list = (JList)e.getSource();
            updateLabel(imageNames[list.getSelectedIndex()]);
        }
       
        //在右边滚动面板的label中显示选择的图片Renders the selected image
        protected void updateLabel (String name) {
            ImageIcon icon = createImageIcon("images/" + name + ".gif");
            picture.setIcon(icon);
            if  (icon != null) {
                picture.setText(null);
            } else {
                picture.setText("Image not found");
            }
        }

        //Used by SplitPaneDemo2
        public JList getImageList() {
            return list;
        }

        public JSplitPane getSplitPane() {
            return splitPane;
        }

      
        /** Returns an ImageIcon, or null if the path was invalid. */
        //在本类所在目录寻找指定路径的图片,找不到就返回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("Couldn't find file: " + path);
                return null;
            }
        }

        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {

            //Create and set up the window.
            JFrame frame = new JFrame("SplitPaneDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
            SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
            frame.getContentPane().add(splitPaneDemo.getSplitPane());

            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }

       
       
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }

         
    }

  • 相关阅读:
    安卓SharedPreferences类的使用
    安卓登录案例
    如何用Android Studio查看build.gradle源码
    学点Groovy来理解build.gradle代码
    build.gradle代码
    【Android】TextView文字长度测量和各种Paddding解析
    【Android】TextView跑马灯效果
    【分享】学长的安利来了~~O(∩_∩)O
    【Android】Fragment懒加载和ViewPager的坑
    【Android】你应该知道的调试神器----adb
  • 原文地址:https://www.cnblogs.com/langtianya/p/2722954.html
Copyright © 2011-2022 走看看