リストの続き
// AttributeSetに選択位置のスタイルを設定する
public MutableAttributeSet setStyles(MutableAttributeSet attr){
String font = StyleConstants.
getFontFamily(selectedAttribute);
StyleConstants.setFontFamily(attr, font);
int size = StyleConstants.
getFontSize(selectedAttribute);
StyleConstants.setFontSize(attr,size);
Color c = StyleConstants.
getForeground(selectedAttribute);
StyleConstants.setForeground(attr,c);
return attr;
}
public static void main(String[] args) {
new FreeMultiFontEditor().setVisible(true);
}
// JTextPaneのCaretListenerクラス
class caretAction implements CaretListener {
public void caretUpdate(CaretEvent ev) {
selstart = textpane.getSelectionStart();
selend = textpane.getSelectionEnd();
StyledDocument doc = textpane.getStyledDocument();
selectedAttribute = doc.getCharacterElement
(selstart).getAttributes();
selstart = textpane.getSelectionStart();
}
}
// <File>メニュー用ActionListenerクラス
class FileMenuAction implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String cmd = ev.getActionCommand();
if (cmd.equals("New")){
newDoc();
} else if (cmd.equals("Open...")){
readFromFile();
} else if (cmd.equals("Save...")){
saveToFile();
} else if (cmd.equals("Quit")){
System.exit(0);
}
}
}
// <Font>メニュー用ActionListenerクラス
class FontMenuAction implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String cmd = ev.getActionCommand();
MutableAttributeSet attr = new SimpleAttributeSet();
attr = setStyles(attr);
StyleConstants.setFontFamily(attr,cmd);
StyledDocument doc = textpane.getStyledDocument();
doc.setCharacterAttributes(selstart,
selend - selstart,attr,true);
textpane.select(selstart,selend);
}
}
// <Size>メニュー用ActionListenerクラス
class SizeMenuAction implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String cmd = ev.getActionCommand();
int n;
try {
n = Integer.parseInt(cmd);
} catch (NumberFormatException e) {
e.printStackTrace();
n = 12;
}
MutableAttributeSet attr = new SimpleAttributeSet();
attr = setStyles(attr);
StyleConstants.setFontSize(attr,n);
StyledDocument doc = textpane.getStyledDocument();
doc.setCharacterAttributes(selstart,
selend - selstart, attr, true);
textpane.select(selstart,selend);
}
}