Java Drawing DrawTop

Language

JP  US  UK

 

フォントメニューボタン

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  

要旨:クリックするとダイアログを表示するボタンを作成する。現在のところフォントメニューだけで使っている。
このページで説明するクラス: ButtonOfFontMenu

1. 概要
他のボタンは、押された時にExecCommandexecメソッドを呼んで。コマンドを実行するのが普通であるが、このボタンは押された時にダイアログを表示するアクションを起こすだけである。コマンド実行アクションは、このダイアログに貼り付けられているボタンやコンボボックスが押されるか選択されたときに起きる。
:プルダウンメニューにComboBoxを貼り付けると、ComboBoxのアイテムを選択できない。ComboBoxの横に表示される縦のスクロールバーのボタンに触れた途端にプルダウンメニューが閉じてしまう。このためボタンを定義し、そのボタンを押すとComboBox、ボタンなどが設定されたダイアログを開くようにする。



図 1. フォントメニューボタンとダイアログ



2. ButtonOfFontMenuクラス 戻る=>page top
public class ButtonOfFontMenu extends JButton implements DrawMenuIF, ActionListener

フィールド
説明
dialog
JDialog dialog
通常ボタンが押されたときに開くフォントメニューダイアログ(図1)を設定する。


メソッド
説明
コンストラクタ (1)
public ButtonOfFontMenu(String commandName, String tip, JDialog dialog)
ボタンにはテキストを表示。
引数:
commandName - コマンド名
tip - tool tipに表示する文字列
dialog - フォントメニューダイアログ
処理:
次のメソッドを呼ぶ。

this.setActionCommand(commandName);
this.addActionListener(this);
this.setName(commandName);
this.setText(commandName);
this.setToolTipText(tip);
this.setStandardButtonStyle();
this.dialog=dialog;

コンストラクタ (2)
public ButtonOfFontMenu(String commandName, boolean setText, ImageIcon imageIcon, String tip, JDialog dialog)
ボタンにはImageIconを表示する。setText=trueならばcommandNameもボタンに表示する。
引数:
commandName - コマンド名
setText - trueならばコマンド名もボタンに表示する。
imageIcon - ボタンに表示するImageIcon.
tip - tool tipに表示する文字列
dialog - フォントメニューダイアログ
処理:
次のメソッドを呼んで設定を行う。

super(imageIcon);
this.setActionCommand(commandName);
this.addActionListener(this);
this.setName(commandName);
if(setText) setText(commandName);
this.setToolTipText(tip);
this.setStandardButtonStyle();
this.dialog=dialog;

setStandardButtonStyle
public void setStandardButtonStyle()
次のメソッドを呼んで設定を行う。

this.setBorder(this.raisedBorder);
this.setIconTextGap(0);
this.setHorizontalTextPosition(SwingConstants.CENTER );
this.setVerticalTextPosition(SwingConstants.BOTTOM );
this.setHorizontalAlignment(CENTER);
this.setFont(MenuConstants.MenuFont);
this.setBackground(null);
this.setForeground(Color.BLACK);

actionPerformed
public void actionPerformed(ActionEvent e)
∙ フォントメニューダイアログを表示する

this.dialog.setVisible(true)

∙ ダイアログの表示位置を設定する。

this.dialog.setLocation(centerP);

createFontMenuButton
public static JButton createFontMenuButton(String commandName, int width, String tip)
引数:
commandName - コマンド名
width - ボタンの幅
width<=0ならばコマンド名のTextLayoutオブジェクトを作成してボタンの幅を決める。
tip - tool tipに表示する文字列
処理:
∙ JDialogオブジェクトを作成

DrawMenucreateFontStyleGroup メソッドでフォントメニューダイアログ(図1)のボタン、コンボボックス、カラーチューザーを作成しダイアログに貼り付ける。
∙ 新しいボタンをコンストラクター(1)で作成して戻り値で返す。



Copyright (c) 2009-2013
All other trademarks are property of their respective owners.