- Home ›
- JFreeChartを使ったグラフ作成 ›
- タイトルの設定 ›
- HERE
フォントの設定(setFont)
広告
タイトルとして表示される文字列のフォントを設定する方法を確認します。TextTitleクラスで用意されている「setFont」メソッドを使います。
setFont public void setFont(java.awt.Font font)
Sets the font used to display the title string. Registered listeners are notified that the title has been modified. Parameters: font - the new font (null not permitted).
引数にはFontクラスの文字列を指定します。
例えば次のように記述します。
JFreeChart chart = ChartFactory.createPieChart(...); # 引数は省略 TextTitle title = new TextTitle("タイトル"); title.setFont(new Font("MS 明朝", Font.BOLD | Font.ITALIC, 24)); chart.setTitle(title);
Fontクラスの詳細な使い方については「Fontクラス」を参照して下さい。
なおTextTitleクラスのコンストラクタの1つにもフォントを指定可能なものがあります。
TextTitle public TextTitle(java.lang.String text, java.awt.Font font)
Creates a new title, using default attributes where necessary. Parameters: text - the title text (null not permitted). font - the title font (null not permitted).
1番目の引数でタイトルとして表示する文字列を指定し、2番目の引数でフォントを指定します。
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import org.jfree.chart.JFreeChart; import org.jfree.chart.ChartFactory; import org.jfree.data.general.DefaultPieDataset; import javax.swing.JFrame; import java.awt.BorderLayout; import org.jfree.chart.ChartPanel; import org.jfree.chart.title.TextTitle; import java.awt.Font; public class Test4_1 extends JFrame{ public static void main(String[] args) { Test4_1 frame = new Test4_1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 500, 500); frame.setTitle("グラフサンプル"); frame.setVisible(true); } Test4_1(){ JFreeChart chart = ChartFactory.createPieChart("", createData(), true, false, false); TextTitle title = new TextTitle("夏の旅行"); title.setFont(new Font("MS 明朝", Font.BOLD | Font.ITALIC, 38)); chart.setTitle(title); ChartPanel cpanel = new ChartPanel(chart); getContentPane().add(cpanel, BorderLayout.CENTER); } private DefaultPieDataset createData(){ DefaultPieDataset data = new DefaultPieDataset(); data.setValue("海外", 30); data.setValue("国内", 60); data.setValue("行かない", 8); data.setValue("未定", 2); return data; } }
上記をコンパイルした後で実行すると次のようにJavaアプリケーションが起動します。
( Written by Tatsuo Ikura )