TOP →
Java →
Swing →
JButton → This Page
JButton@Swing サンプル03
概要
Java -
Swing -
JButton のサンプルです。
・余白を設定
・画像を設定
・画像と文字の余白を設定
解説
ボタン1はボタンと文字列の間の余白を大きめに設定、
ボタン2はアイコン表示、
ボタン3はアイコンと文字列を表示
ボタン4はアイコンと文字列を表示し、アイコンと文字列の余白を大きめに設定しています。
サンプルイメージ
サンプルソース
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JButton サンプル03
* ・余白を設定
* ・画像を設定
* ・画像と文字の余白を設定
*
* @author みっちー
*/
public class JButton03 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JButton03 frame = new JButton03();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JButton サンプル03");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 400, 150);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JButton03() {
// パネルを作成
JPanel panelBase = new JPanel();
// ボタンを作成
JButton button1 = new JButton("ボタン1");
JButton button2 = new JButton(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"));
// 余白を設定
button1.setMargin(new Insets(15, 15, 15, 15));
// 画像と文字の余白を設定
button4.setIconTextGap(50);
// ボタンを追加
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