タイトル
TOPJavaSwingJRadioButton → This Page

JRadioButton@Swing サンプル05

概要

Java - Swing - JRadioButton のサンプルです。
・無効化
・押下画像を設定
・選択画像を設定
・無効画像を設定

解説

ラジオボタン1はアイコンと文字列のボタンです。(特になにもなし)
ラジオボタン2はアイコンと文字列のボタンを無効化、
ラジオボタン3はクリックするとアイコン画像が変化、
ラジオボタン4は無効にするとアイコン画像が変化します。

サンプル画像1はラジオボタン3クリック前、
サンプル画像2はラジオボタン3クリック中(マウスを押してから放すまでの間)です。
サンプル画像3はラジオボタン3クリック後です。

サンプルイメージ

サンプル画像1
サンプル画像2
サンプル画像3


サンプルソース

import javax.swing.ImageIcon;
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * JRadioButton サンプル05
 * ・無効化
 * ・押下画像を設定
 * ・選択画像を設定
 * ・無効画像を設定
 * 
 * @author みっちー
 */
public class JRadioButton05 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JRadioButton05 frame = new JRadioButton05();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JRadioButton サンプル05");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 520, 100);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JRadioButton05() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// ラジオボタンを作成
		JRadioButton radio1 = new JRadioButton("ラジオボタン1", new ImageIcon("./img/java.gif"));
		JRadioButton radio2 = new JRadioButton("ラジオボタン2", new ImageIcon("./img/java.gif"));
		JRadioButton radio3 = new JRadioButton("ラジオボタン3", new ImageIcon("./img/java.gif"));
		JRadioButton radio4 = new JRadioButton("ラジオボタン4", new ImageIcon("./img/java.gif"));
		
		// 無効化
		radio2.setEnabled(false);
		radio4.setEnabled(false);
		
		// 押下画像・選択画像を設定
		radio3.setPressedIcon(new ImageIcon("./img/now.gif"));
		radio3.setSelectedIcon(new ImageIcon("./img/ok.gif"));
		
		// 無効画像を設定
		radio4.setDisabledIcon(new ImageIcon("./img/ng.gif"));
		
		// ラジオボタンを追加
		panelBase.add(radio1);
		panelBase.add(radio2);
		panelBase.add(radio3);
		panelBase.add(radio4);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

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

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

更新履歴

2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2008/01/23 新規作成


TOPJavaSwingJRadioButton → This Page