SWT Tips and Samples 基礎編 > Font

 

Font

Fontクラスを使ってウィジェットのフォントを変更することができます。イディオムとしては下のようになります。コンストラクタでフォント名、サイズ、スタイルを指定します。また生成したフォントは、アプリケーション終了時など何らかの形でdisposeしなければいけません。

Display display = ...;
Font font = new Font(display, "Tahoma", 10, SWT.BOLD | SWT.ITALIC);
Label l = new Label(...);
l.setFont(font);
...
font.dispose();

サンプルプログラム

コンボボックスとラジオボタンでフォントを変更できるプログラムです。

ソースコード (FontTest.java)

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.*;

public class FontTest {

        private Combo comboName;
        private Combo comboSize;
        private Label label;
        private String name = "Tahoma";
        private int size = 10;
        private int style = SWT.NORMAL;
        private Button bBold;
        private Button bItalic;
        private Display display;
        private Font font = null;

        public static void main(String[] args) {
                new FontTest();
        }
        public FontTest() {
                display = new Display();
                Shell shell = new Shell(display);
                shell.setText("FontTest");
                shell.setLayout(new GridLayout(2, false));

                label = new Label(shell, SWT.NONE | SWT.CENTER);
                label.setText("フォントのテスト");
                GridData gd = new GridData(GridData.FILL_BOTH);
                gd.heightHint = 30;
                gd.horizontalSpan = 2;
                label.setLayoutData(gd);
                updateFont();

                comboName = new Combo(shell, SWT.DROP_DOWN | 
                                             SWT.READ_ONLY);
                comboName.add("Tahoma");
                comboName.add("MS ゴシック");
                comboName.add("MS 明朝");
                comboName.add("MS UI Gothic");
                comboName.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                int index = comboName.getSelectionIndex();
                                name = comboName.getItem(index);
                                updateFont();
                        }
                });

                comboSize = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
                comboSize.add("10");
                comboSize.add("12");
                comboSize.add("14");
                comboSize.addSelectionListener(new SelectionAdapter() {
                   public void widgetSelected(SelectionEvent e) {
                      int index = comboSize.getSelectionIndex();
                      size = Integer.parseInt(comboSize.getItem(index));
                      updateFont();
                   }
                });

                bBold = new Button(shell, SWT.CHECK);
                bBold.setText("Bold");
                bBold.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                boolean b = bBold.getSelection();
                                if (b) {
                                        style = style | SWT.BOLD;
                                } else {
                                        style = style | ~SWT.BOLD;
                                }
                                updateFont();
                        }
                });

                bItalic = new Button(shell, SWT.CHECK);
                bItalic.setText("Italic");
                bItalic.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                boolean b = bItalic.getSelection();
                                if (b) {
                                        style = style | SWT.ITALIC;
                                } else {
                                        style = style | ~SWT.ITALIC;
                                }
                                updateFont();
                        }
                });

                shell.pack();
                shell.open();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();
                        }
                }
                if (font != null) {
                        //newしたFontはdisposeする必要がある
                        font.dispose();
                }
                display.dispose();
        }
        private void updateFont() {
                if (font != null) {
                        //newしたFontはdisposeする必要がある
                        font.dispose();
                }
                font = new Font(display, name, size, style);
                label.setFont(font);
        }
}


最新更新日: 2004年9月1日
 
関連リンク
Eclipse API ドキュメント
Eclipse API - Font
FontDialog

- PR -

プレゼンテーション作成ソフト無料お試し版配信中

【Sony】大手他社よりも安い!ビジネス向け光・100Mしかも固定IP付!今なら更に初期費用最大15,000円OFF!

オフィス用品・オフィス家具 価 格 交 渉 可! 
◎ 目指せ★業界最安値 ★ ◎ オフィネット・ドットコム株式会社

注文から納品まで驚きの早さ!!【ASKULカタログ】はこちらから・・・

マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。

■原石のままでは、終わりたくない貴方へ!キャリアアップ転職を成功へとナビゲート

秋葉原・なんば・名古屋・札幌に店舗を構えるパソコンショップ!【ツクモ】

マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。

シマンテック 割引価格! オンライン販売だから低価格。いつでも簡単、見積・購入。

キャンペーンバナー_120_60


Copyright(C) 2003,2004 Jasmin Project. All Right Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送