タイトル
TOPJavaSwingJSplitPane → This Page

JSplitPane@Swing サンプル07

概要

Java - Swing - JSplitPane のサンプルです。
・仕切線の移動可能最小値と最大値を取得
・仕切線の現在位置を取得

解説

[MIN-MAX]ボタンを押すと仕切線の移動可能最小値と最大値を取得してラベルに表示し、
[NOW]ボタンを押すと仕切線の現在位置を取得してラベルに表示します。


サンプルイメージ

サンプル画像
サンプル画像


サンプルソース

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.BevelBorder;

/**
 * JSplitPane サンプル07
 * ・仕切線の移動可能最小値と最大値を取得
 * ・仕切線の現在位置を取得
 * 
 * @author みっちー
 */
public class JSplitPane07 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JSplitPane splitPane;
	private JLabel label;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JSplitPane07 frame = new JSplitPane07();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JSplitPane サンプル07");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 400, 300);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JSplitPane07() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		panelBase.setLayout(new GridLayout(2, 1, 5, 5));
		
		// ボタンを生成
		JButton button1 = new JButton("MIN-MAX");
		JButton button2 = new JButton("NOW");
		
		// アクションコマンドを設定
		button1.setActionCommand("Min");
		button2.setActionCommand("Now");
		
		// リスナーに追加
		button1.addActionListener(this);
		button2.addActionListener(this);
		
		// 分割パネを生成
		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, button1, button2);
		
		// ラベルを生成
		label = new JLabel();
		label.setPreferredSize(new Dimension(100, 50));
		label.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		// 分割パネを追加
		panelBase.add(splitPane);
		panelBase.add(label);
		
		// パネを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 * 
	 * @param e	アクションイベント
	 */
	public void actionPerformed(ActionEvent e){
		if ("Min".equals(e.getActionCommand())) {
			int min = splitPane.getMinimumDividerLocation();
			int max = splitPane.getMaximumDividerLocation();
			label.setText("min=" + min + ", max=" + max);
		} else {
			int location = splitPane.getDividerLocation();
			label.setText("location=" + location);
		}
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/08/25 新規作成


TOPJavaSwingJSplitPane → This Page