JSplitPane

2018-01-09 19:23 更新

Java Swing教程 - JSplitPane


JSplitPane有一個(gè)拆分器來拆分兩個(gè)組件。 分割條可以水平或垂直顯示。

用戶可以向上/向下或向左/向右移動(dòng)分割條,因此一個(gè)組件比另一個(gè)組件獲得更多的空間。

JSplitPane類提供了許多構(gòu)造函數(shù)。 我們可以使用它的默認(rèn)構(gòu)造函數(shù)創(chuàng)建它,并使用它的setTopComponent(Component c)添加兩個(gè)組件,setBottomComponent(Component c),setLeftComponent(Component c),setRightComponent(Component c)。

JSplitPane可以以連續(xù)或非連續(xù)的方式重繪組件。當(dāng)我們改變分割條的位置。

通過調(diào)用JSplitPane的resetToPreferredSizes()方法將分隔符位置重置到該位置。

分隔線位置

使用setDividerLocation(newLocation)更改dividerLocation屬性。0.0和1.0,表示JSplitPane容器寬度的百分比。

import java.awt.BorderLayout;
/*  w w w.j  a  v  a  2s. c o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);

    

    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);

    splitPane.setDividerLocation(0.5);
  }
}


方向

設(shè)置方向:JSplitPane.VERTICAL_SPLIT或JSplitPane.HORIZONTAL_SPLIT

import java.awt.BorderLayout;
/*from   ww  w .j  a v  a 2 s .c  o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    JComponent leftButton = new JButton("Left");
    JComponent rightButton = new JButton("Right");
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setLeftComponent(leftButton);
    splitPane.setRightComponent(rightButton);
    
    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);
    
  }
}


可擴(kuò)展分頻器

調(diào)整組件大小和使用單觸式可擴(kuò)展分隔線

import java.awt.BorderLayout;
/*from   w  ww.jav  a  2 s. c  o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

嵌套JSplitPane

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
//  w w  w  .  j  a  v  a2  s.c  o m
public class Main{
  
  public static void main(String[] a) {
    int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT;

    int VERTSPLIT = JSplitPane.VERTICAL_SPLIT;

    boolean continuousLayout = true;


    JLabel label1 = new JLabel("a");
    JLabel label2 = new JLabel("b");
    JLabel label3 = new JLabel("c");
    JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, label1, label2);
    splitPane1.setOneTouchExpandable(true);
    splitPane1.setDividerSize(2);
    splitPane1.setDividerLocation(0.5);

    JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, label3);
    splitPane2.setOneTouchExpandable(true);
    splitPane2.setDividerLocation(0.4);
    splitPane2.setDividerSize(2);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(splitPane2);
    frame.pack();
    frame.setVisible(true);
  }
}

嵌套JSplitPane...

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
// w  w  w . j a  va2s .  co m
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent changeEvent) {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
          int current = sourceSplitPane.getDividerLocation();
          System.out.println("Current: " + current);
          Integer last = (Integer) changeEvent.getNewValue();
          System.out.println("Last: " + last);
          Integer priorLast = (Integer) changeEvent.getOldValue();
          System.out.println("Prior last: " + priorLast);
        }
      }
    };

    splitPane.addPropertyChangeListener(propertyChangeListener);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)