程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]022、分割一個面板

[JAVA100例]022、分割一個面板

編輯:關於JAVA

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

/**
* <p>Title: 分割面板</p>
* <p>Description: 演示將面板分割成左右兩部分</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>方法說明:構造器,定義了所有要使用的構件
*<br>輸入參數:
*<br>返回類型:
*/
  public SplitPaneDemo() {
   
    //創建一個圖像名稱的列表,設置為單選方式
    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[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);

//創建一個水平分割的面板,定義了兩個面板的名字。
    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
                  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));
  }
/**
*<br>方法說明:獲得這個分割的面板
*<br>輸入參數:
*<br>返回類型:JSplitPane 對象
*/
  public JSplitPane getSplitPane() {
    return splitPane;
  }
/**
*<br>方法說明:列表監聽事件處理
*<br>輸入參數:ListSelectionEvent e 列表選擇事件
*<br>返回類型:
*/
  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>方法說明:根據路徑獲取圖形對象
*<br>輸入參數:String path 圖片路徑
*<br>返回類型:ImageIcon 圖片對象
*/
  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>方法說明:主方法
*<br>輸入參數:
*<br>返回類型:
*/
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

//定義窗體
    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);
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved