📄 selectfontdialog.java
字号:
/* * @(#) SelectFontDialog.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.dialog;//导入核心Java类库import java.awt.Font;import java.awt.Insets;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.GraphicsEnvironment;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JList;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JTextField;import javax.swing.JScrollPane;import javax.swing.DefaultListModel;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;//导入自定义Java类库import hws.item.smart.Smart;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.BorderShop;import hws.item.smart.misc.PopToolkit;/** * 选择字体对话框 * * @version 0.1 2005-05-16 * @author Hwerz */public class SelectFontDialog extends JDialog implements ListSelectionListener { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 静态常量 */ private static final String PLAIN = "常规"; /** * 静态常量 */ private static final String BOLD = "粗体"; /** * 静态常量 */ private static final String ITALIC = "斜体"; /** * 静态常量 */ private static final String BOLD_ITALIC = "粗斜体"; /** * 被选中的字体 */ private static Font font; /** * 字体文本框 */ private JTextField fontNameTextField; /** * 字形文本框 */ private JTextField fontStyleTextField; /** * 字号文本框 */ private JTextField fontSizeTextField; /** * 字体列表 */ private JList fontNameList; /** * 字形列表 */ private JList fontStyleList; /** * 字号列表 */ private JList fontSizeList; /** * 英文示例 */ private JLabel previewLabelEN; /** * 中文示例 */ private JLabel previewLabelCN; /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * Create a new instance of this class * * @param font 初始字体 */ public SelectFontDialog(Font font) { super(Smart.getInstance(), "选择字体", true); this.font = font; Container c = getContentPane(); c.setLayout(new GridBagLayout()); //工具栏面板 GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight GridBagConstraints.REMAINDER, 1, //weightx, weighty 1.0, 0.0, //anchor GridBagConstraints.NORTH, //fill GridBagConstraints.HORIZONTAL, //insets new Insets(5, 0, 0, 0), //ipadx, ipady 0, 0); c.add(new Toolbar(), constraints); //字体标签 constraints.gridy = 1; constraints.gridwidth = 1; constraints.weightx = 0.0; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(5, 5, 0, 0); c.add(new JLabel("字体:"), constraints); //字形标签 constraints.gridx = 1; c.add(new JLabel("字形:"), constraints); //字号标签 constraints.gridx = 2; constraints.insets = new Insets(5, 5, 0, 5); c.add(new JLabel("字号:"), constraints); //字体文本框 fontNameTextField = new JTextField(font.getFamily()); fontNameTextField.setEditable(false); constraints.gridy = 2; constraints.gridx = 0; constraints.weightx = 0.4; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.insets = new Insets(0, 5, 0, 0); c.add(fontNameTextField, constraints); //字形文本框 fontStyleTextField = new JTextField(getFontStyle(font)); fontStyleTextField.setEditable(false); constraints.gridx = 1; constraints.weightx = 0.3; c.add(fontStyleTextField, constraints); //字号文本框 fontSizeTextField = new JTextField(String.valueOf(font.getSize())); fontSizeTextField.setEditable(false); constraints.gridx = 2; constraints.insets = new Insets(0, 5, 0, 5); c.add(fontSizeTextField, constraints); //字体列表 DefaultListModel fontNameListModel = new DefaultListModel(); String[] fontNames = getAllFontNames(); for (int i = 0; i < fontNames.length; i++) { fontNameListModel.addElement(fontNames[i]); } fontNameList = new JList(fontNameListModel); fontNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fontNameList.setSelectedValue(font.getFamily(), true); fontNameList.addListSelectionListener(this); JScrollPane scroller = new JScrollPane(fontNameList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridy = 3; constraints.gridx = 0; constraints.weightx = 0.4; constraints.weighty = 0.8; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 0, 0); c.add(scroller, constraints); //字形列表 DefaultListModel fontStyleListModel = new DefaultListModel(); fontStyleListModel.addElement(PLAIN); fontStyleListModel.addElement(BOLD); fontStyleListModel.addElement(ITALIC); fontStyleListModel.addElement(BOLD_ITALIC); fontStyleList = new JList(fontStyleListModel); fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fontStyleList.setSelectedValue(getFontStyle(font), true); fontStyleList.addListSelectionListener(this); scroller = new JScrollPane(fontStyleList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridx = 1; constraints.weightx = 0.3; c.add(scroller, constraints); //字号列表 DefaultListModel fontSizeListModel = new DefaultListModel(); for (int i = 8; i <= 28; i = i + 2) { fontSizeListModel.addElement(String.valueOf(i)); } fontSizeListModel.addElement("36"); fontSizeListModel.addElement("48"); fontSizeListModel.addElement("72"); fontSizeList = new JList(fontSizeListModel); fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fontSizeList.setSelectedValue(String.valueOf(font.getSize()), true); fontSizeList.addListSelectionListener(this); scroller = new JScrollPane(fontSizeList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridx = 2; constraints.insets = new Insets(5, 5, 0, 5); c.add(scroller, constraints); //英文示例 previewLabelEN = new JLabel("This is a sample"); previewLabelEN.setFont(font); previewLabelEN.setHorizontalAlignment(JLabel.CENTER); previewLabelEN.setBorder(BorderShop.EN_SAMPLE_BORDER); scroller = new JScrollPane(previewLabelEN, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroller.setBorder(null); constraints.gridy = 4; constraints.gridx = 0; constraints.weightx = 0.5; constraints.weighty = 0.2; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 0, 0); c.add(scroller, constraints); //中文示例 previewLabelCN = new JLabel("这是一个例子"); previewLabelCN.setFont(font); previewLabelCN.setHorizontalAlignment(JLabel.CENTER); previewLabelCN.setBorder(BorderShop.CN_SAMPLE_BORDER); scroller = new JScrollPane(previewLabelCN, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroller.setBorder(null); constraints.gridx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.insets = new Insets(5, 5, 0, 5); c.add(scroller, constraints); //设置对话框 setSize(600, 450); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); PopToolkit.makeWindowCenter(this); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 返回选中的字体 * * @return 选中的字体 */ public static Font getSelectedFont() { return font; } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 返回指定Font对象的风格 * * @param font 指定的Font对象 * @return 指定Font对象的风格 */ private String getFontStyle(Font font) { String style = null; if (font.isPlain() == true) { style = PLAIN; } else if (font.isBold() == true) { if (font.isItalic() == true) { style = BOLD_ITALIC; } else { style = BOLD; } } else { style = ITALIC; } return style; } /** * 返回当前系统的所有可用字体 * * @return 当前系统的所有可用字体 */ private String[] getAllFontNames() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontNames[] = ge.getAvailableFontFamilyNames(); return fontNames; } /** * 创建字体对象 * * @return 创建的字体对象 */ private Font createFont() { int fontStyle; if (fontStyleTextField.getText().equals(PLAIN)) { fontStyle = Font.PLAIN; } else if (fontStyleTextField.getText().equals(BOLD)) { fontStyle = Font.BOLD; } else if (fontStyleTextField.getText().equals(ITALIC)) { fontStyle = Font.ITALIC; } else { fontStyle = Font.BOLD + Font.ITALIC; } return new Font(fontNameTextField.getText(), fontStyle, Integer.parseInt(fontSizeTextField.getText())); } /*------------------------------------------------------------------------* * 实现方法 * *------------------------------------------------------------------------*/ /** * 实现接口ListSelectionListener的方法 * * @param event ListSelectionEvent对象 */ public void valueChanged(ListSelectionEvent event) { JList list = (JList) event.getSource(); if (list == fontNameList) { fontNameTextField.setText(list.getSelectedValue().toString()); } else if (list == fontStyleList) { fontStyleTextField.setText(list.getSelectedValue().toString()); } else { fontSizeTextField.setText(list.getSelectedValue().toString()); } Font font = createFont(); previewLabelEN.setFont(font); previewLabelCN.setFont(font); } /*------------------------------------------------------------------------* * 内部类 * *------------------------------------------------------------------------*/ /** * 工具栏面板 */ class Toolbar extends JPanel implements ActionListener { /** * Create a new instance of this class */ public Toolbar() { super(new FlowLayout(FlowLayout.CENTER, 5, 0)); //确定 JButton button = new JButton("确定", ImageShop.OK_IMAGEICON); button.addActionListener(this); add(button); //取消 button = new JButton("取消", ImageShop.CANCEL_IMAGEICON); button.addActionListener(this); add(button); } /** * 实现接口ActionListener的方法 * * @param event the event that characterizes the action */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("确定") == true) { font = createFont(); SelectFontDialog.this.dispose(); } else { SelectFontDialog.this.dispose(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -