タイトル
TOPJavaSwingJSeparator → This Page

JSeparator@Swing サンプル01

概要

Java - Swing - JSeparator のサンプルです。
・色々なコンストラクタ
・サイズを指定
・無効化
・色を設定
・枠を設定
・ツールチップを設定

解説

色々なコンストラクタを使用してセパレータを作成しています。

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

/**
 * JSeparator サンプル01
 * ・色々なコンストラクタ
 * ・サイズを指定
 * ・無効化
 * ・色を設定
 * ・枠を設定
 * ・ツールチップを設定
 * 
 * @author みっちー
 */
public class JSeparator01 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JSeparator01 frame = new JSeparator01();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JSeparator サンプル01");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 740, 160);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JSeparator01() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// セパレータを生成
		JSeparator separator1 = new JSeparator();
		JSeparator separator2 = new JSeparator(JSeparator.VERTICAL);
		JSeparator separator3 = new JSeparator(JSeparator.HORIZONTAL);
		JSeparator separator4 = new JSeparator(JSeparator.VERTICAL);
		JSeparator separator5 = new JSeparator(JSeparator.VERTICAL);
		
		// サイズを指定
		separator1.setPreferredSize(new Dimension(50, 10));
		separator2.setPreferredSize(new Dimension(10, 100));
		separator3.setPreferredSize(new Dimension(50, 10));
		separator4.setPreferredSize(new Dimension(10, 100));
		separator5.setPreferredSize(new Dimension(10, 100));
		
		// 無効化
		separator3.setEnabled(false);
		
		// 色を設定
		separator4.setForeground(Color.RED);
		separator4.setBackground(Color.BLUE);
		
		// 枠を設定
		Border border = new BevelBorder(BevelBorder.RAISED);
		separator5.setBorder(border);
		
		// ツールチップを設定
		separator1.setToolTipText("セパレータ1のツールチップです。");
		separator2.setToolTipText("セパレータ2のツールチップです。");
		separator3.setToolTipText("セパレータ3のツールチップです。");
		separator4.setToolTipText("セパレータ4のツールチップです。");
		separator5.setToolTipText("セパレータ5のツールチップです。");
		
		// ボタンを生成
		JButton button1 = new JButton("ボタン1");
		JButton button2 = new JButton("ボタン2");
		JButton button3 = new JButton("ボタン3");
		JButton button4 = new JButton("ボタン4");
		JButton button5 = new JButton("ボタン5");
		JButton button6 = new JButton("ボタン6");
		
		// ボタン、セパレータを追加
		panelBase.add(button1);
		panelBase.add(separator1);
		panelBase.add(button2);
		panelBase.add(separator2);
		panelBase.add(button3);
		panelBase.add(separator3);
		panelBase.add(button4);
		panelBase.add(separator4);
		panelBase.add(button5);
		panelBase.add(separator5);
		panelBase.add(button6);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

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

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

更新履歴

2016/05/13 新規作成


TOPJavaSwingJSeparator → This Page