タイトル
TOPJavaSwingJButton → This Page

JButton@Swing サンプル04

概要

Java - Swing - JButton のサンプルです。
・画像に対する文字の位置を設定

解説

ボタンのアイコンに対する文字列の位置を設定しています。

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Dimension;

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

/**
 * JButton サンプル04
 * ・画像に対する文字の位置を設定
 * 
 * @author みっちー
 */
public class JButton04 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JButton04 frame = new JButton04();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JButton サンプル04");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 500, 300);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JButton04() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// ボタンを作成
		JButton button1 = new JButton("ボタン1", new ImageIcon("./img/pic.gif"));
		JButton button2 = new JButton("ボタン2", new ImageIcon("./img/pic.gif"));
		JButton button3 = new JButton("ボタン3", new ImageIcon("./img/pic.gif"));
		JButton button4 = new JButton("ボタン4", new ImageIcon("./img/pic.gif"));
		JButton button5 = new JButton("ボタン5", new ImageIcon("./img/pic.gif"));
		JButton button6 = new JButton("ボタン6", new ImageIcon("./img/pic.gif"));
		JButton button7 = new JButton("ボタン7", new ImageIcon("./img/pic.gif"));
		JButton button8 = new JButton("ボタン8", new ImageIcon("./img/pic.gif"));
		JButton button9 = new JButton("ボタン9", new ImageIcon("./img/pic.gif"));
		
		// サイズを設定
		button1.setPreferredSize(new Dimension(150, 70));
		button2.setPreferredSize(new Dimension(150, 70));
		button3.setPreferredSize(new Dimension(150, 70));
		button4.setPreferredSize(new Dimension(150, 70));
		button5.setPreferredSize(new Dimension(150, 70));
		button6.setPreferredSize(new Dimension(150, 70));
		button7.setPreferredSize(new Dimension(150, 70));
		button8.setPreferredSize(new Dimension(150, 70));
		button9.setPreferredSize(new Dimension(150, 70));
		
		// 文字列水平位置を設定
		button1.setHorizontalTextPosition(JButton.LEFT);
		button2.setHorizontalTextPosition(JButton.CENTER);
		button3.setHorizontalTextPosition(JButton.RIGHT);
		button4.setHorizontalTextPosition(JButton.LEFT);
		button5.setHorizontalTextPosition(JButton.CENTER);
		button6.setHorizontalTextPosition(JButton.RIGHT);
		button7.setHorizontalTextPosition(JButton.LEFT);
		button8.setHorizontalTextPosition(JButton.CENTER);
		button9.setHorizontalTextPosition(JButton.RIGHT);
		
		// 文字列垂直位置を設定
		button1.setVerticalTextPosition(JButton.TOP);
		button2.setVerticalTextPosition(JButton.TOP);
		button3.setVerticalTextPosition(JButton.TOP);
		button4.setVerticalTextPosition(JButton.CENTER);
		button5.setVerticalTextPosition(JButton.CENTER);
		button6.setVerticalTextPosition(JButton.CENTER);
		button7.setVerticalTextPosition(JButton.BOTTOM);
		button8.setVerticalTextPosition(JButton.BOTTOM);
		button9.setVerticalTextPosition(JButton.BOTTOM);
		
		// ボタンを追加
		panelBase.add(button1);
		panelBase.add(button2);
		panelBase.add(button3);
		panelBase.add(button4);
		panelBase.add(button5);
		panelBase.add(button6);
		panelBase.add(button7);
		panelBase.add(button8);
		panelBase.add(button9);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

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

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

更新履歴

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


TOPJavaSwingJButton → This Page