TOP →
Java →
Swing →
JToggleButton → This Page
JToggleButton@Swing サンプル02
概要
Java -
Swing -
JToggleButton のサンプルです。
・水平位置を設定
・垂直位置を設定
解説
トグルボタンの文字列の表示位置を設定しています。
サンプルイメージ
サンプルソース
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JToggleButton サンプル02
* ・水平位置を設定
* ・垂直位置を設定
*
* @author みっちー
*/
public class JToggleButton02 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JToggleButton02 frame = new JToggleButton02();
// 閉じるトグルボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JToggleButton サンプル02");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 500, 220);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JToggleButton02() {
// パネルを作成
JPanel panelBase = new JPanel();
// トグルボタンを作成
JToggleButton tbutton1 = new JToggleButton("トグルボタン1");
JToggleButton tbutton2 = new JToggleButton("トグルボタン2");
JToggleButton tbutton3 = new JToggleButton("トグルボタン3");
JToggleButton tbutton4 = new JToggleButton("トグルボタン4");
JToggleButton tbutton5 = new JToggleButton("トグルボタン5");
JToggleButton tbutton6 = new JToggleButton("トグルボタン6");
JToggleButton tbutton7 = new JToggleButton("トグルボタン7");
JToggleButton tbutton8 = new JToggleButton("トグルボタン8");
JToggleButton tbutton9 = new JToggleButton("トグルボタン9");
// サイズを設定
tbutton1.setPreferredSize(new Dimension(150, 50));
tbutton2.setPreferredSize(new Dimension(150, 50));
tbutton3.setPreferredSize(new Dimension(150, 50));
tbutton4.setPreferredSize(new Dimension(150, 50));
tbutton5.setPreferredSize(new Dimension(150, 50));
tbutton6.setPreferredSize(new Dimension(150, 50));
tbutton7.setPreferredSize(new Dimension(150, 50));
tbutton8.setPreferredSize(new Dimension(150, 50));
tbutton9.setPreferredSize(new Dimension(150, 50));
// 水平位置を設定
tbutton1.setHorizontalAlignment(JToggleButton.LEFT);
tbutton2.setHorizontalAlignment(JToggleButton.CENTER);
tbutton3.setHorizontalAlignment(JToggleButton.RIGHT);
tbutton4.setHorizontalAlignment(JToggleButton.LEFT);
tbutton5.setHorizontalAlignment(JToggleButton.CENTER);
tbutton6.setHorizontalAlignment(JToggleButton.RIGHT);
tbutton7.setHorizontalAlignment(JToggleButton.LEFT);
tbutton8.setHorizontalAlignment(JToggleButton.CENTER);
tbutton9.setHorizontalAlignment(JToggleButton.RIGHT);
// 垂直位置を設定
tbutton1.setVerticalAlignment(JToggleButton.TOP);
tbutton2.setVerticalAlignment(JToggleButton.TOP);
tbutton3.setVerticalAlignment(JToggleButton.TOP);
tbutton4.setVerticalAlignment(JToggleButton.CENTER);
tbutton5.setVerticalAlignment(JToggleButton.CENTER);
tbutton6.setVerticalAlignment(JToggleButton.CENTER);
tbutton7.setVerticalAlignment(JToggleButton.BOTTOM);
tbutton8.setVerticalAlignment(JToggleButton.BOTTOM);
tbutton9.setVerticalAlignment(JToggleButton.BOTTOM);
// 分かりやすいように背景色を設定
tbutton1.setBackground(Color.LIGHT_GRAY);
tbutton2.setBackground(Color.LIGHT_GRAY);
tbutton3.setBackground(Color.LIGHT_GRAY);
tbutton4.setBackground(Color.LIGHT_GRAY);
tbutton5.setBackground(Color.LIGHT_GRAY);
tbutton6.setBackground(Color.LIGHT_GRAY);
tbutton7.setBackground(Color.LIGHT_GRAY);
tbutton8.setBackground(Color.LIGHT_GRAY);
tbutton9.setBackground(Color.LIGHT_GRAY);
// トグルボタンを追加
panelBase.add(tbutton1);
panelBase.add(tbutton2);
panelBase.add(tbutton3);
panelBase.add(tbutton4);
panelBase.add(tbutton5);
panelBase.add(tbutton6);
panelBase.add(tbutton7);
panelBase.add(tbutton8);
panelBase.add(tbutton9);
// パネルを追加
getContentPane().add(panelBase);
}
}
サンプルソースのダウンロード
ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)
更新履歴
2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2008/01/23 新規作成
TOP →
Java →
Swing →
JToggleButton → This Page