TOP →
Java →
Swing →
JToggleButton → This Page
JToggleButton@Swing サンプル04
概要
Java -
Swing -
JToggleButton のサンプルです。
・画像に対する文字の位置を設定
解説
トグルボタンのアイコンに対する文字列の位置を設定しています。
サンプルイメージ
サンプルソース
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* JToggleButton サンプル04
* ・画像に対する文字の位置を設定
*
* @author みっちー
*/
public class JToggleButton04 extends JFrame {
private static final long serialVersionUID = 1L;
/**
* 開始メソッド
*
* @param args パラメータ
*/
public static void main(String[] args) {
JToggleButton04 frame = new JToggleButton04();
// 閉じるボタンをクリックされた場合の動作を設定
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ウインドウのタイトルを設定
frame.setTitle("JToggleButton サンプル04");
// フレームの X座標、Y座標、幅、高さを設定
frame.setBounds(100, 200, 500, 280);
// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
frame.setVisible(true);
}
/**
* コンストラクタ
*/
public JToggleButton04() {
// パネルを作成
JPanel panelBase = new JPanel();
// トグルボタンを作成
JToggleButton tbutton1 = new JToggleButton("トグルボタン1", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton2 = new JToggleButton("トグルボタン2", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton3 = new JToggleButton("トグルボタン3", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton4 = new JToggleButton("トグルボタン4", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton5 = new JToggleButton("トグルボタン5", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton6 = new JToggleButton("トグルボタン6", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton7 = new JToggleButton("トグルボタン7", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton8 = new JToggleButton("トグルボタン8", new ImageIcon("./img/pic.gif"));
JToggleButton tbutton9 = new JToggleButton("トグルボタン9", new ImageIcon("./img/pic.gif"));
// サイズを設定
tbutton1.setPreferredSize(new Dimension(150, 70));
tbutton2.setPreferredSize(new Dimension(150, 70));
tbutton3.setPreferredSize(new Dimension(150, 70));
tbutton4.setPreferredSize(new Dimension(150, 70));
tbutton5.setPreferredSize(new Dimension(150, 70));
tbutton6.setPreferredSize(new Dimension(150, 70));
tbutton7.setPreferredSize(new Dimension(150, 70));
tbutton8.setPreferredSize(new Dimension(150, 70));
tbutton9.setPreferredSize(new Dimension(150, 70));
// 文字列水平位置を設定
tbutton1.setHorizontalTextPosition(JToggleButton.LEFT);
tbutton2.setHorizontalTextPosition(JToggleButton.CENTER);
tbutton3.setHorizontalTextPosition(JToggleButton.RIGHT);
tbutton4.setHorizontalTextPosition(JToggleButton.LEFT);
tbutton5.setHorizontalTextPosition(JToggleButton.CENTER);
tbutton6.setHorizontalTextPosition(JToggleButton.RIGHT);
tbutton7.setHorizontalTextPosition(JToggleButton.LEFT);
tbutton8.setHorizontalTextPosition(JToggleButton.CENTER);
tbutton9.setHorizontalTextPosition(JToggleButton.RIGHT);
// 文字列垂直位置を設定
tbutton1.setVerticalTextPosition(JToggleButton.TOP);
tbutton2.setVerticalTextPosition(JToggleButton.TOP);
tbutton3.setVerticalTextPosition(JToggleButton.TOP);
tbutton4.setVerticalTextPosition(JToggleButton.CENTER);
tbutton5.setVerticalTextPosition(JToggleButton.CENTER);
tbutton6.setVerticalTextPosition(JToggleButton.CENTER);
tbutton7.setVerticalTextPosition(JToggleButton.BOTTOM);
tbutton8.setVerticalTextPosition(JToggleButton.BOTTOM);
tbutton9.setVerticalTextPosition(JToggleButton.BOTTOM);
// トグルボタンを追加
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