タイトル
TOPJavaSwingJDesktopPane,JInternalFrame → This Page

JDesktopPane,JInternalFrame@Swing サンプル04

概要

Java - Swing - JDesktopPane,JInternalFrame のサンプルです。
・内部フレームの位置を設定
・内部フレームのサイズを設定
・内部フレームのタイトルを設定
・内部フレームのサイズ変更有無を設定
・内部フレームのクローズ有無を設定
・内部フレームの最大化有無を設定
・内部フレームのアイコン化有無を設定

解説

[Location]ボタンのクリックで内部フレームの位置を設定、
[Size]ボタンのクリックで内部フレームのサイズを設定、
[Title]ボタンのクリックで内部フレームのタイトルを設定、
[resizable]ボタンのクリックで内部フレームのサイズ変更有無を設定、
[closable]ボタンのクリックで内部フレームのクローズ有無を設定、
[maximizable]ボタンのクリックで内部フレームの最大化有無を設定、
[iconifiable]ボタンのクリックで内部フレームのアイコン化有無を設定します。

サンプルイメージ

起動時
サンプル画像

全てのボタンをクリックした後
サンプル画像


サンプルソース

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

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 * JDesktopPane サンプル04
 * ・内部フレームの位置を設定
 * ・内部フレームのサイズを設定
 * ・内部フレームのタイトルを設定
 * ・内部フレームのサイズ変更有無を設定
 * ・内部フレームのクローズ有無を設定
 * ・内部フレームの最大化有無を設定
 * ・内部フレームのアイコン化有無を設定
 * 
 * @author みっちー
 */
public class JDesktopPane04 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JInternalFrame iframe;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JDesktopPane04 frame = new JDesktopPane04();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JDesktopPane サンプル04");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 480, 300);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JDesktopPane04() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		panelBase.setLayout(new BoxLayout(panelBase, BoxLayout.Y_AXIS));
		JPanel panelSub = new JPanel();
		panelSub.setLayout(new GridLayout(2, 4, 5, 5));
		
		// デスクトップパネを生成
		JDesktopPane desktop = new JDesktopPane();
		
		// 内部フレームを生成
		iframe = new JInternalFrame();
		
		// 内部フレームのサイズと位置をセット
		iframe.setLocation(10, 10);
		iframe.setSize(200, 100);
		iframe.setBounds(10, 10, 200, 100);
		
		// 内部フレームのタイトルをセット
		iframe.setTitle("title");
		
		// 内部フレームを表示
		iframe.setVisible(true);
		
		// デスクトップパネに内部フレームを追加
		desktop.add(iframe);
		
		// ボタンを作成
		JButton button1 = createButton("Location");
		JButton button2 = createButton("Size");
		JButton button3 = createButton("Title");
		JButton button4 = createButton("resizable");
		JButton button5 = createButton("closable");
		JButton button6 = createButton("maximizable");
		JButton button7 = createButton("iconifiable");
		
		// ボタンをパネルに追加
		panelSub.add(button1);
		panelSub.add(button2);
		panelSub.add(button3);
		panelSub.add(button4);
		panelSub.add(button5);
		panelSub.add(button6);
		panelSub.add(button7);
		
		// デスクトップパネとサブパネルをメインパネルに追加
		panelBase.add(desktop);
		panelBase.add(panelSub);
		
		// メインパネルを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		if ("Location".equals(e.getActionCommand())) {
			// 内部フレームの位置を設定
			iframe.setLocation(100, 50);
		} else if ("Size".equals(e.getActionCommand())) {
			// 内部フレームのサイズを設定
			iframe.setSize(250, 120);
		} else if ("Title".equals(e.getActionCommand())) {
			// 内部フレームのタイトルを設定
			iframe.setTitle("new title");
		} else if ("resizable".equals(e.getActionCommand())) {
			// 内部フレームのサイズ変更有無を設定
			iframe.setResizable(true);
		} else if ("closable".equals(e.getActionCommand())) {
			// 内部フレームのクローズ有無を設定
			iframe.setClosable(true);
		} else if ("maximizable".equals(e.getActionCommand())) {
			// 内部フレームの最大化有無を設定
			iframe.setMaximizable(true);
		} else if ("iconifiable".equals(e.getActionCommand())) {
			// 内部フレームのアイコン化有無を設定
			iframe.setIconifiable(true);
		}
	}
	
	/**
	 * ボタンを生成
	 * 
	 * @param title	タイトル、アクションコマンド
	 * @return
	 */
	private JButton createButton(String title) {
		// ボタンを作成
		JButton button = new JButton(title);
		
		// アクションコマンドを設定
		button.setActionCommand(title);
		
		// アクションリスナー追加
		button.addActionListener(this);
		
		return button;
	}
}

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

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

更新履歴

2016/08/27 新規作成


TOPJavaSwingJDesktopPane,JInternalFrame → This Page