タイトル
TOPJavaSwingJRadioButton → This Page

JRadioButton@Swing サンプル02

概要

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

解説

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

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * JRadioButton サンプル02
 * ・水平位置を設定
 * ・垂直位置を設定
 * 
 * @author みっちー
 */
public class JRadioButton02 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 */
	public static void main(String[] args) {
		JRadioButton02 frame = new JRadioButton02();
		
		// 閉じるラジオボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JRadioButton サンプル02");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 500, 220);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 */
	public JRadioButton02() {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// ラジオボタンを作成
		JRadioButton radio1 = new JRadioButton("ラジオボタン1");
		JRadioButton radio2 = new JRadioButton("ラジオボタン2");
		JRadioButton radio3 = new JRadioButton("ラジオボタン3");
		JRadioButton radio4 = new JRadioButton("ラジオボタン4");
		JRadioButton radio5 = new JRadioButton("ラジオボタン5");
		JRadioButton radio6 = new JRadioButton("ラジオボタン6");
		JRadioButton radio7 = new JRadioButton("ラジオボタン7");
		JRadioButton radio8 = new JRadioButton("ラジオボタン8");
		JRadioButton radio9 = new JRadioButton("ラジオボタン9");
		
		// サイズを設定
		radio1.setPreferredSize(new Dimension(150, 50));
		radio2.setPreferredSize(new Dimension(150, 50));
		radio3.setPreferredSize(new Dimension(150, 50));
		radio4.setPreferredSize(new Dimension(150, 50));
		radio5.setPreferredSize(new Dimension(150, 50));
		radio6.setPreferredSize(new Dimension(150, 50));
		radio7.setPreferredSize(new Dimension(150, 50));
		radio8.setPreferredSize(new Dimension(150, 50));
		radio9.setPreferredSize(new Dimension(150, 50));
		
		// 水平位置を設定
		radio1.setHorizontalAlignment(JRadioButton.LEFT);
		radio2.setHorizontalAlignment(JRadioButton.CENTER);
		radio3.setHorizontalAlignment(JRadioButton.RIGHT);
		radio4.setHorizontalAlignment(JRadioButton.LEFT);
		radio5.setHorizontalAlignment(JRadioButton.CENTER);
		radio6.setHorizontalAlignment(JRadioButton.RIGHT);
		radio7.setHorizontalAlignment(JRadioButton.LEFT);
		radio8.setHorizontalAlignment(JRadioButton.CENTER);
		radio9.setHorizontalAlignment(JRadioButton.RIGHT);
		
		// 垂直位置を設定
		radio1.setVerticalAlignment(JRadioButton.TOP);
		radio2.setVerticalAlignment(JRadioButton.TOP);
		radio3.setVerticalAlignment(JRadioButton.TOP);
		radio4.setVerticalAlignment(JRadioButton.CENTER);
		radio5.setVerticalAlignment(JRadioButton.CENTER);
		radio6.setVerticalAlignment(JRadioButton.CENTER);
		radio7.setVerticalAlignment(JRadioButton.BOTTOM);
		radio8.setVerticalAlignment(JRadioButton.BOTTOM);
		radio9.setVerticalAlignment(JRadioButton.BOTTOM);
		
		// 分かりやすいように背景色を設定
		radio1.setBackground(Color.LIGHT_GRAY);
		radio2.setBackground(Color.LIGHT_GRAY);
		radio3.setBackground(Color.LIGHT_GRAY);
		radio4.setBackground(Color.LIGHT_GRAY);
		radio5.setBackground(Color.LIGHT_GRAY);
		radio6.setBackground(Color.LIGHT_GRAY);
		radio7.setBackground(Color.LIGHT_GRAY);
		radio8.setBackground(Color.LIGHT_GRAY);
		radio9.setBackground(Color.LIGHT_GRAY);
		
		// ラジオボタンを追加
		panelBase.add(radio1);
		panelBase.add(radio2);
		panelBase.add(radio3);
		panelBase.add(radio4);
		panelBase.add(radio5);
		panelBase.add(radio6);
		panelBase.add(radio7);
		panelBase.add(radio8);
		panelBase.add(radio9);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

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

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

更新履歴

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


TOPJavaSwingJRadioButton → This Page