1. 程式人生 > >JSplitPane學習的小例子

JSplitPane學習的小例子

package test;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Container;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
public class JSplitPaneDemo{
 public JSplitPaneDemo(){
  JFrame frame = new JFrame("JSplitPaneDemo");
  Container contentPane = frame.getContentPane();
  JLabel label1 = new JLabel("label1",JLabel.CENTER);
  label1.setBackground(Color.GREEN);
  label1.setOpaque(true);
  JLabel label2 = new JLabel("label2",JLabel.CENTER);
  label2.setBackground(Color.PINK);
  label2.setOpaque(true);
  JLabel label3 = new JLabel("label3",JLabel.CENTER);
  label3.setBackground(Color.YELLOW);
  label3.setOpaque(true);

  //將label1和label2加入到splitPane1中
  //並設定splitPane1為水平分割(左右分割),splitPane1具有Continuous layout的功能
  JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,label1,label2);
  //設定splitPane1水平分割線的位置
  splitPane1.setDividerLocation(0.3);
  splitPane1.setOneTouchExpandable(true);//設定splitPane是否可以展開或收起
  splitPane1.setDividerSize(10);//設定分割線寬度的大小

  JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,splitPane1,label3);
  splitPane2.setDividerLocation(35);
  splitPane2.setOneTouchExpandable(true);
  splitPane2.setDividerSize(5);

  contentPane.add(splitPane2);
  frame.setSize(250,200);
  frame.show();
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
 }
 public static void main(String[] args){
  new JSplitPaneDemo();
 }
}