プログラムのダウンロード
ダウンロード開始
ソースコード
※FreeMultiFontEditor.java
package jp.tuyano.allabout;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.*;
import javax.swing.text.rtf.RTFEditorKit;
import java.lang.reflect.*;
public class FreeMultiFontEditor extends JFrame {
private static final long serialVersionUID = 1L;
private JTextPane textpane;
private AttributeSet selectedAttribute;
private int selstart,selend;
// コンストラクタ。基本コンポーネントの準備
public FreeMultiFontEditor(){
this.setLayout(new BorderLayout());
createMenuBar();
textpane = new JTextPane();
StyleContext style = new StyleContext();
DefaultStyledDocument doc =
new DefaultStyledDocument(style);
textpane.setDocument(doc);
textpane.addCaretListener(new caretAction());
JScrollPane spane = new JScrollPane(textpane);
this.add(spane,BorderLayout.CENTER);
this.newDoc();
this.setSize(new Dimension(400,400));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// メニューバーの組み込み
public void createMenuBar(){
JMenuBar menubar = new JMenuBar();
// file menu
JMenu fmenu = new JMenu("File");
menubar.add(fmenu);
FileMenuAction fAction = new FileMenuAction();
JMenuItem new_menu = new JMenuItem("New");
new_menu.addActionListener(fAction);
fmenu.add(new_menu);
JMenuItem open_menu = new JMenuItem("Open...");
open_menu.addActionListener(fAction);
fmenu.add(open_menu);
JMenuItem save_menu = new JMenuItem("Save...");
save_menu.addActionListener(fAction);
fmenu.add(save_menu);
fmenu.addSeparator();
JMenuItem quit_menu = new JMenuItem("Quit");
quit_menu.addActionListener(fAction);
fmenu.add(quit_menu);
// font menu
JMenu ftmenu = new JMenu("Font");
menubar.add(ftmenu);
FontMenuAction ftaction = new FontMenuAction();
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
String fontarr[] = env.getAvailableFontFamilyNames();
for (int i = 0; i < fontarr.length; i++) {
JMenuItem family_menu = new JMenuItem(fontarr[i]);
family_menu.addActionListener(ftaction);
ftmenu.add(family_menu);
}
// size menu
String[] narr = new String[]{"9","12","18",
"24","36","48"};
JMenu smenu = new JMenu("Size");
menubar.add(smenu);
SizeMenuAction saction = new SizeMenuAction();
for (String str : narr) {
JMenuItem size_menu = new JMenuItem(str);
size_menu.addActionListener(saction);
smenu.add(size_menu);
}
// color menu
String[] carr = new String[]{"BLACK","RED","GREEN",
"BLUE","CYAN","MAGENTA","YELLOW","GRAY"};
JMenu cmenu = new JMenu("color");
menubar.add(cmenu);
ColorMenuAction staction = new ColorMenuAction();
for(String str : carr){
JMenuItem color_menu = new JMenuItem(str);
color_menu.addActionListener(staction);
cmenu.add(color_menu);
}
this.setJMenuBar(menubar);
}
// <New>メニューの処理
public void newDoc(){
StyleContext style = new StyleContext();
DefaultStyledDocument doc =
new DefaultStyledDocument(style);
textpane.setDocument(doc);
}
// <Open>メニューの処理
public void readFromFile(){
StyleContext style = new StyleContext();
DefaultStyledDocument doc =
new DefaultStyledDocument(style);
RTFEditorKit rtfkit = new RTFEditorKit();
File file = null;
JFileChooser chooser = new JFileChooser();
MyRTFFilter filter = new MyRTFFilter();
chooser.setFileFilter(filter);
int res = chooser.showOpenDialog(this);
if (res == JFileChooser.APPROVE_OPTION){
try {
file = chooser.getSelectedFile();
InputStream stream = new FileInputStream(file);
rtfkit.read(stream, doc, 0);
stream.close();
textpane.setDocument(doc);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
// <Save>メニューの処理
public void saveToFile(){
Document doc = textpane.getDocument();
RTFEditorKit rtfEditor = new RTFEditorKit();
File file = null;
JFileChooser chooser = new JFileChooser();
int res = chooser.showSaveDialog(this);
if (res == JFileChooser.APPROVE_OPTION){
try {
file = chooser.getSelectedFile();
OutputStream out = new FileOutputStream(file);
rtfEditor.write(out,doc,0,doc.getLength());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}