?? selectfontdialog.java
字號(hào):
/* * @(#) SelectFontDialog.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.dialog;//導(dǎo)入核心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;//導(dǎo)入自定義Java類庫import hws.item.smart.Smart;import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.BorderShop;import hws.item.smart.misc.PopToolkit;/** * 選擇字體對(duì)話框 * * @version 0.1 2005-05-16 * @author Hwerz */public class SelectFontDialog extends JDialog implements ListSelectionListener { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 靜態(tài)常量 */ private static final String PLAIN = "常規(guī)"; /** * 靜態(tài)常量 */ private static final String BOLD = "粗體"; /** * 靜態(tài)常量 */ private static final String ITALIC = "斜體"; /** * 靜態(tài)常量 */ private static final String BOLD_ITALIC = "粗斜體"; /** * 被選中的字體 */ private static Font font; /** * 字體文本框 */ private JTextField fontNameTextField; /** * 字形文本框 */ private JTextField fontStyleTextField; /** * 字號(hào)文本框 */ private JTextField fontSizeTextField; /** * 字體列表 */ private JList fontNameList; /** * 字形列表 */ private JList fontStyleList; /** * 字號(hào)列表 */ private JList fontSizeList; /** * 英文示例 */ private JLabel previewLabelEN; /** * 中文示例 */ private JLabel previewLabelCN; /*------------------------------------------------------------------------* * 構(gòu)造函數(shù) * *------------------------------------------------------------------------*/ /** * 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); //字體標(biāo)簽 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); //字形標(biāo)簽 constraints.gridx = 1; c.add(new JLabel("字形:"), constraints); //字號(hào)標(biāo)簽 constraints.gridx = 2; constraints.insets = new Insets(5, 5, 0, 5); c.add(new JLabel("字號(hào):"), 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); //字號(hào)文本框 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); //字號(hào)列表 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("這是一個(gè)例子"); 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); //設(shè)置對(duì)話框 setSize(600, 450); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); PopToolkit.makeWindowCenter(this); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 返回選中的字體 * * @return 選中的字體 */ public static Font getSelectedFont() { return font; } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 返回指定Font對(duì)象的風(fēng)格 * * @param font 指定的Font對(duì)象 * @return 指定Font對(duì)象的風(fēng)格 */ 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; } /** * 返回當(dāng)前系統(tǒng)的所有可用字體 * * @return 當(dāng)前系統(tǒng)的所有可用字體 */ private String[] getAllFontNames() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontNames[] = ge.getAvailableFontFamilyNames(); return fontNames; } /** * 創(chuàng)建字體對(duì)象 * * @return 創(chuàng)建的字體對(duì)象 */ 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())); } /*------------------------------------------------------------------------* * 實(shí)現(xiàn)方法 * *------------------------------------------------------------------------*/ /** * 實(shí)現(xiàn)接口ListSelectionListener的方法 * * @param event ListSelectionEvent對(duì)象 */ 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); } /*------------------------------------------------------------------------* * 內(nèi)部類 * *------------------------------------------------------------------------*/ /** * 工具欄面板 */ 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); } /** * 實(shí)現(xiàn)接口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(); } } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -