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