Swing/ResourceMenuBar のバックアップ差分(No.18)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ResourceMenuBar へ行く。
- 1 (2004-02-26 (木) 02:48:55)
- 2 (2004-03-11 (木) 15:48:26)
- 3 (2004-06-02 (水) 09:57:28)
- 4 (2004-07-23 (金) 10:55:55)
- 5 (2004-08-17 (火) 08:57:05)
- 6 (2004-10-06 (水) 06:49:32)
- 7 (2004-10-08 (金) 06:23:53)
- 8 (2004-11-04 (木) 10:09:34)
- 9 (2005-02-03 (木) 02:04:16)
- 10 (2005-04-28 (木) 04:32:56)
- 11 (2005-06-26 (日) 12:45:04)
- 12 (2005-10-01 (土) 22:28:57)
- 13 (2006-02-27 (月) 16:20:09)
- 14 (2006-11-14 (火) 13:58:46)
- 15 (2007-08-24 (金) 16:43:44)
- 16 (2009-07-08 (水) 16:18:23)
- 17 (2013-02-26 (火) 14:52:35)
- 18 (2013-10-19 (土) 22:04:39)
- 19 (2015-01-14 (水) 15:41:47)
- 20 (2015-11-26 (木) 04:24:32)
- 21 (2017-05-26 (金) 16:35:28)
- 22 (2018-02-22 (木) 14:02:24)
- 23 (2018-10-14 (日) 16:45:31)
- 24 (2018-12-28 (金) 15:24:23)
- 25 (2020-11-25 (水) 00:58:02)
- 26 (2022-08-20 (土) 22:15:25)
- 27 (2023-01-06 (金) 17:29:20)
- 28 (2024-02-02 (金) 12:21:37)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:Resourceファイルからメニューバーを生成 #navi(../) #tags() RIGHT:Posted by &author(aterai); at 2003-09-25 *Resourceファイルからメニューバーを生成 [#mc12a27d] リソースファイルを使ってメニューバーやツールバーを生成します。詳しくは ''%JAVA_HOME%/demo/jfc/Notepad/src/Notepad.java'' を参照してください。 #tags(JMenuBar, JMenu, JMenuItem, Properties, ResourceBundle) RIGHT:Posted by &author(aterai); at 2003-10-06 * Resourceファイルからメニューバーを生成 [#mc12a27d] リソースファイルを使ってメニューバーやツールバーを生成します。詳しくは`%JAVA_HOME%/demo/jfc/Notepad/src/Notepad.java`を参照してください。 -&jnlp; -&jar; -&zip; - &jnlp; - &jar; - &zip; //#screenshot #ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTR15q_ELI/AAAAAAAAAh0/2H6dW1g0eiY/s800/ResourceMenuBar.png) #ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTR15q_ELI/AAAAAAAAAh0/2H6dW1g0eiY/s800/ResourceMenuBar.png) **サンプルコード [#t11a6578] ** サンプルコード [#t11a6578] #code(link){{ public JMenuBar createMenubar() { JMenuBar mb = new JMenuBar(); String[] menuKeys = tokenize(getResourceString("menubar")); for(int i=0;i<menuKeys.length;i++) { JMenu m = createMenu(menuKeys[i]); if(m != null) { mb.add(m); } } return mb; } private JMenu createMenu(String key) { String[] itemKeys = tokenize(getResourceString(key)); String mitext = getResourceString(key + labelSuffix); JMenu menu = new JMenu(mitext); String mn = getResourceString(key + mneSuffix); if(mn!=null) { String tmp = mn.toUpperCase().trim(); if(tmp.length()==1) { if(mitext.indexOf(tmp)<0) { menu.setText(mitext+" ("+tmp+")"); } byte[] bt = tmp.getBytes(); menu.setMnemonic((int) bt[0]); } } for(int i=0;i<itemKeys.length;i++) { if(itemKeys[i].equals("-")) { menu.addSeparator(); }else{ JMenuItem mi = createMenuItem(itemKeys[i]); menu.add(mi); } } menus.put(key, menu); return menu; } private JMenuItem createMenuItem(String cmd) { String mitext = getResourceString(cmd + labelSuffix); JMenuItem mi = new JMenuItem(mitext); URL url = getResource(cmd+imageSuffix); if(url!=null) { mi.setHorizontalTextPosition(JButton.RIGHT); mi.setIcon(new ImageIcon(url)); } String astr = getResourceString(cmd + actionSuffix); if(astr == null) { astr = cmd; } String mn = getResourceString(cmd + mneSuffix); if(mn!=null) { String tmp = mn.toUpperCase().trim(); if(tmp.length()==1) { if(mitext.indexOf(tmp)<0) { mi.setText(mitext+" ("+tmp+")"); } byte[] bt = tmp.getBytes(); mi.setMnemonic((int) bt[0]); } } mi.setActionCommand(astr); Action a = getAction(astr); if(a!=null) { mi.addActionListener(a); //a.addPropertyChangeListener(createActionChangeListener(mi)); mi.setEnabled(a.isEnabled()); }else{ mi.setEnabled(false); } menuItems.put(cmd, mi); return mi; } public JMenuItem getMenuItem(String cmd) { return (JMenuItem) menuItems.get(cmd); } public JMenu getMenu(String cmd) { return (JMenu) menus.get(cmd); } public Action getAction(String cmd) { return (Action) commands.get(cmd); } public Action[] getActions() { return actions; } }} **解説 [#ae955fd5] ** 解説 [#ae955fd5] アプリケーションの起動時に、リソースファイルからメニューのテキストの生成、アイコン、ショートカットなどの指定を行います。 上記のサンプルでは、バージョンと終了しか機能しないので、このページにあるsrc.zipの"Main.properties.utf8"、"Main_ja_JP.properties.utf8"(日本語用)といったリソースファイルを編集したり、新しいリソースファイルを作成してみてください。 上記のサンプルでは、バージョンと終了しか機能しないので、このページにある`src.zip`の`Main.properties.utf8`、`Main_ja_JP.properties.utf8`(日本語用)といったリソースファイルを編集したり、新しいリソースファイルを作成してみてください。 ソースコードの方では以下のように"defaultActions"に、上記のpropertiesファイルに書いたActionを追加します。 ソースコードの方では以下のように`defaultActions`に、上記の`properties`ファイルに書いた`Action`を追加します。 #code{{ public Action[] defaultActions={ new NewAction(), //new OpenAction(), new ExitAction(), new HelpAction(), new VersionAction(), }; }} リソースファイルで日本語などをそのまま使用することは出来ないので、以下のように ant から native2ascii でユニコードエスケープしています。 リソースファイルで日本語などをそのまま使用することは出来ないので、以下のように`ant`から`native2ascii`でユニコードエスケープしています。 #code{{ <condition property="have.resources"> <available file="${res.dir}" /> </condition> <target name="prepare-resource" depends="prepare" if="have.resources"> <mkdir dir="${build.res}" /> <native2ascii encoding="UTF-8" src="${res.dir}" dest="${build.res}" includes="**/*.properties.utf8" ext="" /> <copy todir="${build.res}"> <fileset dir="${res.dir}" excludes="**/*.properties.*, **/*.bak" /> </copy> </target> }} 面倒な場合は、JDK 1.5 での、XML に対応した Properties を使用するようにしてみてください。 面倒な場合は、`JDK 1.5`での、`XML`に対応した`Properties`を使用するようにしてみてください。 -参考: [http://www.javainthebox.net/laboratory/J2SE1.5/TinyTips/Properties/Properties.html J2SE 5.0 Tiger 虎の穴 Properties] - 参考: [http://www.javainthebox.net/laboratory/J2SE1.5/TinyTips/Properties/Properties.html J2SE 5.0 Tiger 虎の穴 Properties] ---- JDK 1.6 なら、native2asciiで変換しなくても、リソースファイルのエンコードを指定して読み込むことが出来ます。 `JDK 1.6`なら、`native2ascii`で変換しなくても、リソースファイルのエンコードを指定して読み込むことが出来ます。 -参考 --[http://d.hatena.ne.jp/shin/20090707/p4 Java 小ネタ千夜一夜 第13夜 Java SE 6はnative2ascii使わなくていいのはみんな知っていると思うが] --[http://docs.oracle.com/javase/jp/6/api/java/util/ResourceBundle.Control.html ResourceBundle.Control (Java Platform SE 6)] - 参考 -- [http://d.hatena.ne.jp/shin/20090707/p4 Java 小ネタ千夜一夜 第13夜 Java SE 6はnative2ascii使わなくていいのはみんな知っていると思うが] -- [http://docs.oracle.com/javase/jp/6/api/java/util/ResourceBundle.Control.html ResourceBundle.Control (Java Platform SE 6)] #code{{ ResourceBundle res = ResourceBundle.getBundle(baseName, new ResourceBundle.Control() { public java.util.List<String> getFormats(String baseName) { if (baseName == null) throw new NullPointerException(); return Arrays.asList("properties"); } public ResourceBundle newBundle( String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { if (baseName == null || locale == null || format == null || loader == null) throw new NullPointerException(); ResourceBundle bundle = null; if (format.equals("properties")) { String bundleName = toBundleName(baseName, locale); String resourceName = toResourceName(bundleName, format); InputStream stream = null; if (reload) { URL url = loader.getResource(resourceName); if (url != null) { URLConnection connection = url.openConnection(); if (connection != null) { connection.setUseCaches(false); stream = connection.getInputStream(); } } } else { stream = loader.getResourceAsStream(resourceName); } if (stream != null) { //BufferedInputStream bis = new BufferedInputStream(stream); Reader r = new BufferedReader(new InputStreamReader(stream, "UTF-8")); bundle = new PropertyResourceBundle(r); r.close(); } } return bundle; } }); }} //**参考リンク **コメント [#u53b57cb] ** コメント [#u53b57cb] #comment