タイトル
TOPJavaSwingJButton → This Page

JButton@Swing サンプル02

概要

Java - Swing - JButton のサンプルです。
・水平位置を設定
・垂直位置を設定

解説

ボタンの文字列の表示位置を設定しています。

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Dimension;

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

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