📄 fontdialog.java
字号:
package notebook;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JEditorPane;
import java.awt.List;
import java.awt.Dimension;
import java.awt.Button;
import java.awt.TextField;
import java.awt.Color;
import javax.swing.UIManager;
import java.awt.Font;
import java.awt.Choice;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.nio.charset.Charset;
public class FontDialog extends JDialog implements ItemListener, ActionListener,
TextListener {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JTextField jtxtFontName = new JTextField();
List listFontName = new List();
JLabel jLabel2 = new JLabel();
List listFontStyle = new List();
JTextField jtxtFontStyle = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jtxtFontSize = new JTextField();
List listFontSize = new List(4, false);
Button btnOk = new Button();
Button btnCancel = new Button();
JLabel jLabel4 = new JLabel();
TextField txtDemo = new TextField();
JLabel jLabel5 = new JLabel();
int fontSizeMin = 8, fontSizeMax = 72, fontSizeChangedStep = 1;
NoteBook mainFrame = null;
int fontStyleInt = 0;
Choice choiceCharset = new Choice();
public FontDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public FontDialog(Frame parent) {
super(parent);
try {
mainFrame = (NoteBook) parent;
initFontName(mainFrame.textArea.getFont());
initFontStyle(mainFrame.textArea.getFont());
initFontSize(mainFrame.textArea.getFont());
Charset defaultCharset=java.nio.charset.Charset.defaultCharset();
this.choiceCharset.add(defaultCharset.name());
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public FontDialog() {
this(new Frame(), "FontDialog", false);
}
private void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
this.setResizable(false);
this.setTitle("字体");
jLabel1.setText("字体(F):");
jLabel2.setText("字形(Y):");
panel1.setPreferredSize(new Dimension(400, 300));
jLabel3.setText("大小(S):");
btnOk.setLabel("确定");
btnCancel.setLabel("取消");
jLabel4.setText("示例");
txtDemo.setBackground(UIManager.getColor("Button.background"));
txtDemo.setFont(new java.awt.Font("仿宋_GB2312", Font.ITALIC, 12));
txtDemo.setForeground(Color.black);
txtDemo.setLocale(new java.util.Locale("zh", "", ""));
txtDemo.setText("演示字体");
jLabel5.setText("字符集(R):");
panel1.add(jLabel1, new XYConstraints(19, 20, 77, 17));
panel1.add(jtxtFontName, new XYConstraints(21, 39, 124, -1));
panel1.add(jLabel2, new XYConstraints(166, 21, -1, -1));
panel1.add(jLabel3, new XYConstraints(256, 22, -1, -1));
panel1.add(listFontStyle, new XYConstraints(151, 66, 98, 116));
panel1.add(listFontSize, new XYConstraints(253, 66, 62, 116));
panel1.add(listFontName, new XYConstraints(20, 66, 127, 116));
panel1.add(jtxtFontStyle, new XYConstraints(150, 39, 98, -1));
panel1.add(jtxtFontSize, new XYConstraints(253, 39, 63, -1));
panel1.add(btnOk, new XYConstraints(322, 37, 59, 25));
panel1.add(btnCancel, new XYConstraints(322, 67, 59, -1));
panel1.add(txtDemo, new XYConstraints(198, 195, 136, 36));
panel1.add(jLabel5, new XYConstraints(163, 238, -1, -1));
panel1.add(jLabel4, new XYConstraints(163, 188, -1, -1));
panel1.add(choiceCharset, new XYConstraints(165, 259, 168, -1));
panel1.setBackground(SystemColor.control);
this.getContentPane().add(panel1, java.awt.BorderLayout.SOUTH);
btnCancel.addActionListener(this);
listFontSize.addItemListener(this);
listFontName.addItemListener(this);
listFontStyle.addItemListener(this);
btnOk.addActionListener(this);
}
//初始化字体尺寸列表框
private void initFontSize(Font font) {
int indexOfList = 0;
boolean breakHere = false;
for (int i = fontSizeMin; i <= fontSizeMax; i = i + fontSizeChangedStep) {
if (!breakHere) {
if (i != font.getSize()) {
indexOfList++;
} else {
breakHere = true;
}
}
listFontSize.add(i + "");
}
if (indexOfList < 0
|| indexOfList > (fontSizeMax - fontSizeMin)
/ fontSizeChangedStep + 1) {
listFontSize.select(0);
} else {
listFontSize.select(indexOfList);
}
this.jtxtFontSize.setText(font.getSize() + "");
}
//初始化字体名称列表框
private void initFontName(Font font) {
String[] s = GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for (int i = 0; i < s.length; i++) {
this.listFontName.add(s[i].toString(), i);
if (font.getName().equals(s[i].toString())) {
this.listFontName.select(i);
}
}
this.jtxtFontName.setText(font.getName());
}
//初始化字形列表框
private void initFontStyle(Font font) {
this.listFontStyle.add("常规", Font.PLAIN);
this.listFontStyle.add("斜体", Font.ITALIC);
this.listFontStyle.add("粗体", Font.BOLD);
this.listFontStyle.add("粗斜体", Font.BOLD + Font.ITALIC);
this.listFontStyle.select(font.getStyle());
this.jtxtFontStyle.setText(listFontStyle.getSelectedItem().toString());
}
//返回字体
public Font returnFont() {
return new Font(listFontName.getSelectedItem().toString(), fontStyleInt,
Integer
.parseInt(listFontSize.getSelectedItem()));
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCancel) {
this.dispose();
} else if (e.getSource() == btnOk) {
mainFrame.textArea.setFont(this.returnFont());
this.dispose();
}
}
public void textValueChanged(TextEvent e) {
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == listFontSize) {
this.jtxtFontSize.setText(listFontSize.getSelectedItem().toString());
} else if (e.getSource() == listFontName) {
this.jtxtFontName.setText(listFontName.getSelectedItem().toString());
} else if (e.getSource() == listFontStyle) {
this.jtxtFontStyle.setText(listFontStyle.getSelectedItem().toString());
fontStyleInt = listFontStyle.getSelectedIndex();
}
this.txtDemo.setFont(this.returnFont());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -