タイトル
TOPJavaSwingJDesktopPane,JInternalFrame → This Page

JDesktopPane,JInternalFrame@Swing サンプル08

概要

Java - Swing - JDesktopPane,JInternalFrame のサンプルです。
・内部フレームを選択

解説

特定の内部フレームを選択します。
サンプルではボタンをクリックすると内部フレーム3を選択します。

サンプルイメージ

起動時
サンプル画像

ボタンクリック後
サンプル画像


サンプルソース

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

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 サンプル08
 * ・内部フレームを選択
 * 
 * @author みっちー
 */
public class JDesktopPane08 extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JInternalFrame iframe1;
	private JInternalFrame iframe2;
	private JInternalFrame iframe3;
	private JInternalFrame iframe4;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JDesktopPane08 frame = new JDesktopPane08();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JDesktopPane サンプル08");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 400, 300);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JDesktopPane08() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		panelBase.setLayout(new BoxLayout(panelBase, BoxLayout.Y_AXIS));
		
		// デスクトップパネを生成
		JDesktopPane desktop = new JDesktopPane();
		
		// 内部フレームを生成
		iframe1 = new JInternalFrame("title1");
		iframe2 = new JInternalFrame("title2");
		iframe3 = new JInternalFrame("title3");
		iframe4 = new JInternalFrame("title4");
		
		// 内部フレームのサイズと位置をセット
		iframe1.setBounds(10, 10, 200, 100);
		iframe2.setBounds(40, 40, 200, 100);
		iframe3.setBounds(70, 70, 200, 100);
		iframe4.setBounds(100, 100, 200, 100);
		
		// 内部フレームを表示
		iframe1.setVisible(true);
		iframe2.setVisible(true);
		iframe3.setVisible(true);
		iframe4.setVisible(true);
		
		// デスクトップパネに内部フレームを追加
		desktop.add(iframe1);
		desktop.add(iframe2);
		desktop.add(iframe3);
		desktop.add(iframe4);
		
		// ボタンを作成
		JButton button = new JButton("SELECT 3");
		button.addActionListener(this);
		
		// デスクトップパネとボタンをパネルに追加
		panelBase.add(desktop);
		panelBase.add(button);
		
		// パネを追加
		getContentPane().add(panelBase);
	}
	
	/**
	 * アクション
	 */
	public void actionPerformed(ActionEvent e) {
		// 内部フレームを選択
		try {
			iframe3.setSelected(true);
		} catch (PropertyVetoException ex) {
			ex.printStackTrace();
		}
	}
	
}

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

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

更新履歴

2016/08/27 新規作成


TOPJavaSwingJDesktopPane,JInternalFrame → This Page