📄 fontchooser.java
字号:
package note;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FontChooser extends JDialog {
private static final long serialVersionUID = -3630912355924172054L;
String family;
String fontName;
int fontStyle;
int fontSize;
int style;
int size;
Font selectedFont;
String[] fontFamilies;
ItemChooser families, styles, sizes;
JTextArea preview;
JButton okay, cancel;
static final String[] styleNames = new String[] { "Plain", "Italic",
"Bold", "BoldItalic" };
static final Integer[] styleValues = new Integer[] {
new Integer(Font.PLAIN), new Integer(Font.ITALIC),
new Integer(Font.BOLD), new Integer(Font.BOLD + Font.ITALIC) };
static final String[] sizeNames = new String[] { "8", "10", "12", "14",
"18", "20", "24", "28", "32", "40", "48", "56", "64", "72" };
static final String defaultPreviewString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
+ "abcdefghijklmnopqrstuvwxyz\n"
+ "1234567890!@#$%^&*()_-=+[]{}<,.>\n"
+ "The quick brown fox jumps over the lazy dog\n" +
"这是字体设置....本行只能在中文字体下显示\n" ;
public FontChooser(Frame owner) {
super(owner, "字体选择");
setModal(true);
GraphicsEnvironment env = GraphicsEnvironment
.getLocalGraphicsEnvironment();
fontFamilies = env.getAvailableFontFamilyNames();
family = fontFamilies[fontFamilies.length-1];
style = Font.PLAIN;
size = 18;
selectedFont = new Font(family, style, size);
families = new ItemChooser("字体", fontFamilies, null, 0,
ItemChooser.COMBOBOX);
styles = new ItemChooser("字形", styleNames, styleValues, 0,
ItemChooser.COMBOBOX);
sizes = new ItemChooser("大小", sizeNames, null, 4,
ItemChooser.COMBOBOX);
families.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontFamily((String) e.getSelectedValue());
}
});
styles.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontStyle(((Integer) e.getSelectedValue()).intValue());
}
});
sizes.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontSize(Integer.parseInt((String) e.getSelectedValue()));
}
});
preview = new JTextArea(defaultPreviewString, 5, 40);
preview.setFont(selectedFont);
okay = new JButton("确定");
cancel = new JButton("取消");
okay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println(selectedFont);
Note.fontConf(selectedFont);//设置note 的字体
setVisible(false);
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedFont = null;
setVisible(false);
}
});
Box choosersBox = Box.createHorizontalBox();
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(families);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(styles);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(sizes);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(Box.createGlue());
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Box.createGlue());
buttonBox.add(okay);
buttonBox.add(Box.createGlue());
buttonBox.add(cancel);
buttonBox.add(Box.createGlue());
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(preview), BorderLayout.CENTER);
contentPane.add(choosersBox, BorderLayout.NORTH);
contentPane.add(buttonBox, BorderLayout.SOUTH);
pack();
}
public Font getSelectedFont() {
return selectedFont;
}
public String getFontFamily() {
return family;
}
public int getFontStyle() {
return style;
}
public int getFontSize() {
return size;
}
public void setFontFamily(String name) {
family = name;
changeFont();
}
public void setFontStyle(int style) {
this.style = style;
changeFont();
}
public void setFontSize(int size) {
this.size = size;
changeFont();
}
public void setSelectedFont(Font font) {
selectedFont = font;
family = font.getFamily();
style = font.getStyle();
size = font.getSize();
preview.setFont(font);
}
protected void changeFont() {
selectedFont = new Font(family, style, size);
preview.setFont(selectedFont);
}
public boolean isModal() {
return true;
}
}
class ItemChooser extends JPanel {
private static final long serialVersionUID = 1L;
String name;
String[] labels;
Object[] values;
int selection;
int presentation;
public static final int LIST = 1;
public static final int COMBOBOX = 2;
public static final int RADIOBUTTONS = 3;
JList list;
JComboBox combobox;
JRadioButton[] radiobuttons;
ArrayList listeners = new ArrayList();
public ItemChooser(String name, String[] labels, Object[] values,
int defaultSelection, int presentation) {
this.name = name;
this.labels = labels;
this.values = values;
this.selection = defaultSelection;
this.presentation = presentation;
if (values == null)
this.values = labels;
switch (presentation) {
case LIST:
initList();
break;
case COMBOBOX:
initComboBox();
break;
case RADIOBUTTONS:
initRadioButtons();
break;
}
}
void initList() {
list = new JList(labels);
list.setSelectedIndex(selection);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
ItemChooser.this.select(list.getSelectedIndex());
}
});
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel(name));
this.add(new JScrollPane(list));
}
void initComboBox() {
combobox = new JComboBox(labels);
combobox.setSelectedIndex(selection);
combobox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
ItemChooser.this.select(combobox.getSelectedIndex());
}
});
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(new JLabel(name));
this.add(combobox);
}
void initRadioButtons() {
radiobuttons = new JRadioButton[labels.length];
ButtonGroup radioButtonGroup = new ButtonGroup();
ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JRadioButton b = (JRadioButton) e.getSource();
if (b.isSelected()) {
for (int i = 0; i < radiobuttons.length; i++) {
if (radiobuttons[i] == b) {
ItemChooser.this.select(i);
return;
}
}
}
}
};
this.setBorder(new TitledBorder(new EtchedBorder(), name));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
for (int i = 0; i < labels.length; i++) {
radiobuttons[i] = new JRadioButton(labels[i]);
if (i == selection)
radiobuttons[i].setSelected(true);
radiobuttons[i].addChangeListener(listener);
radioButtonGroup.add(radiobuttons[i]);
this.add(radiobuttons[i]);
}
}
public String getName() {
return name;
}
public int getPresentation() {
return presentation;
}
public String[] getLabels() {
return labels;
}
public Object[] getValues() {
return values;
}
public int getSelectedIndex() {
return selection;
}
public Object getSelectedValue() {
return values[selection];
}
public void setSelectedIndex(int selection) {
switch (presentation) {
case LIST:
list.setSelectedIndex(selection);
break;
case COMBOBOX:
combobox.setSelectedIndex(selection);
break;
case RADIOBUTTONS:
radiobuttons[selection].setSelected(true);
break;
}
this.selection = selection;
}
protected void select(int selection) {
this.selection = selection;
if (!listeners.isEmpty()) {
ItemChooser.Event e = new ItemChooser.Event(this, selection,
values[selection]);
for (Iterator i = listeners.iterator(); i.hasNext();) {
ItemChooser.Listener l = (ItemChooser.Listener) i.next();
l.itemChosen(e);
}
}
}
@SuppressWarnings("unchecked")
public void addItemChooserListener(ItemChooser.Listener l) {
listeners.add(l);
}
public void removeItemChooserListener(ItemChooser.Listener l) {
listeners.remove(l);
}
public static class Event extends java.util.EventObject {
/**
*
*/
private static final long serialVersionUID = -5186582821442425217L;
int selectedIndex;
Object selectedValue;
public Event(ItemChooser source, int selectedIndex, Object selectedValue) {
super(source);
this.selectedIndex = selectedIndex;
this.selectedValue = selectedValue;
}
public ItemChooser getItemChooser() {
return (ItemChooser) getSource();
}
public int getSelectedIndex() {
return selectedIndex;
}
public Object getSelectedValue() {
return selectedValue;
}
}
public interface Listener extends java.util.EventListener {
public void itemChosen(ItemChooser.Event e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -