TOP →
Java →
Swing →
JToggleButton → This Page
JToggleButton@Swing サンプル05
概要
Java -
Swing -
JToggleButton のサンプルです。
・無効化
・押下画像を設定
・選択画像を設定
・無効画像を設定
解説
トグルボタン1はアイコンと文字列のボタンです。(特になにもなし)
トグルボタン2はアイコンと文字列のボタンを無効化、
トグルボタン3はクリックするとアイコン画像が変化、
トグルボタン4は無効にするとアイコン画像が変化します。
サンプル画像1はトグルボタン3クリック前、
サンプル画像2はトグルボタン3クリック中(マウスを押してから放すまでの間)です。
サンプル画像3はトグルボタン3クリック後です。
サンプルイメージ
サンプルソース
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JToggleButton サンプル05
* ・無効化
* ・押下画像を設定
* ・選択画像を設定
* ・無効画像を設定
*
* @author みっちー
*/
public class JToggleButton05 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JToggleButton05 frame = new JToggleButton05();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JToggleButton サンプル05");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 400, 120);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JToggleButton05() {
// パネルを作成
JPanel panelBase = new JPanel();
// トグルボタンを作成
JToggleButton tbutton1 = new JToggleButton("トグルボタン1", new ImageIcon("./img/java.gif"));
JToggleButton tbutton2 = new JToggleButton("トグルボタン2", new ImageIcon("./img/java.gif"));
JToggleButton tbutton3 = new JToggleButton("トグルボタン3", new ImageIcon("./img/java.gif"));
JToggleButton tbutton4 = new JToggleButton("トグルボタン4", new ImageIcon("./img/java.gif"));
// 無効化
tbutton2.setEnabled(false);
tbutton4.setEnabled(false);
// 押下画像・選択画像を設定
tbutton3.setPressedIcon(new ImageIcon("./img/now.gif"));
tbutton3.setSelectedIcon(new ImageIcon("./img/ok.gif"));
// 無効画像を設定
tbutton4.setDisabledIcon(new ImageIcon("./img/ng.gif"));
// トグルボタンを追加
panelBase.add(tbutton1);
panelBase.add(tbutton2);
panelBase.add(tbutton3);
panelBase.add(tbutton4);
// パネルを追加
getContentPane().add(panelBase);
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2008/01/23 新規作成
TOP →
Java →
Swing →
JToggleButton → This Page