表示だけの画面
情報を表示するだけの画面がある。今までは JLabel を何個も貼り付けていたけど、画面のデザインを変更しようとするたびにコードを書き換える必要があった。
なんのことはない。表示するだけなら JEditorPane を使えばよかった。JEditorPane にHTMLソースを渡してやれば、適当にレンダリングしてくれる。つまりコードの書き換えは不要で、HTMLソースを書き換えれば表示する文字列や配置などを変更できる。
HTMLソースは適当なファイルから読み込めばいいが、ResourceBundleから取ってくるようにしておけば、国際化対応も楽にできたりする。
もちろん、内部に可変文字列を埋め込みたい場合はMessageFormatを使えばいい。
ResourceBundle info = ResourceBundle.getBundle("info"); String format = info.getString("html-source"); MessageFormat formatter = new MessageFormat(format); Object o[] = new Object[n]; o[0] = variable; : String html = formatter.format(o); JEditorPane ep = new JEditorPane(); ep.setContentType("text/html"); ep.setEditable(false); ep.setText(html);