?? fontchooserwithpole.java
字號:
package qq.client.panel;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Graphics;import java.awt.GraphicsEnvironment;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.font.TextAttribute;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.ButtonGroup;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ListCellRenderer;import javax.swing.ListModel;import javax.swing.ToolTipManager;import javax.swing.border.CompoundBorder;import javax.swing.border.EtchedBorder;import javax.swing.border.LineBorder;import javax.swing.border.MatteBorder;import javax.swing.border.TitledBorder;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.text.AttributeSet;import javax.swing.text.MutableAttributeSet;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;import static java.awt.font.TextAttribute.*;/** * * @author fubin www.cujava.com * * 這是一個非常重要的字體選擇窗口,非常完整的程序。實現(xiàn)任意保存,直接使用就可以了。 * */public class FontChooserWithPole extends JDialog { protected int Closed_Option = JOptionPane.CLOSED_OPTION; protected InputList fontNameInputList = new InputList(fontNames, "Name:"); protected InputList fontSizeInputList = new InputList(fontSizes, "Size:"); protected MutableAttributeSet attributes; protected JCheckBox boldCheckBox = new JCheckBox("Bold"); protected JCheckBox italicCheckBox = new JCheckBox("Italic"); protected JCheckBox underlineCheckBox = new JCheckBox("Underline"); protected JCheckBox strikethroughCheckBox = new JCheckBox("Strikethrough"); protected JCheckBox subscriptCheckBox = new JCheckBox("Subscript"); protected JCheckBox superscriptCheckBox = new JCheckBox("Superscript"); protected ColorComboBox colorComboBox; protected FontLabel previewLabel; public static String[] fontNames; public static String[] fontSizes; private static final String PREVIEW_TEXT = "Preview Font"; public FontChooserWithPole(JFrame owner) { super(owner, "字體選擇窗口", false); getContentPane().setLayout( new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); JPanel p = new JPanel(new GridLayout(1, 2, 10, 2)); p.setBorder(new TitledBorder(new EtchedBorder(), "字體")); p.add(fontNameInputList); fontNameInputList.setDisplayedMnemonic('n'); fontNameInputList.setToolTipText("Font name"); p.add(fontSizeInputList); fontSizeInputList.setDisplayedMnemonic('s'); fontSizeInputList.setToolTipText("Font size"); getContentPane().add(p); p = new JPanel(new GridLayout(2, 3, 10, 5)); p.setBorder(new TitledBorder(new EtchedBorder(), "樣式")); boldCheckBox.setMnemonic('b'); boldCheckBox.setToolTipText("Bold font"); p.add(boldCheckBox); italicCheckBox.setMnemonic('i'); italicCheckBox.setToolTipText("Italic font"); p.add(italicCheckBox); underlineCheckBox.setMnemonic('u'); underlineCheckBox.setToolTipText("Underline font"); p.add(underlineCheckBox); strikethroughCheckBox.setMnemonic('r'); strikethroughCheckBox.setToolTipText("Strikethrough font"); p.add(strikethroughCheckBox); subscriptCheckBox.setMnemonic('t'); subscriptCheckBox.setToolTipText("Subscript font"); p.add(subscriptCheckBox); superscriptCheckBox.setMnemonic('p'); superscriptCheckBox.setToolTipText("Superscript font"); p.add(superscriptCheckBox); getContentPane().add(p); getContentPane().add(Box.createVerticalStrut(5)); p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); p.add(Box.createHorizontalStrut(10)); JLabel lbl = new JLabel("顏色:"); lbl.setDisplayedMnemonic('c'); p.add(lbl); p.add(Box.createHorizontalStrut(20)); colorComboBox = new ColorComboBox(); lbl.setLabelFor(colorComboBox); colorComboBox.setToolTipText("Font color"); ToolTipManager.sharedInstance().registerComponent(colorComboBox); p.add(colorComboBox); p.add(Box.createHorizontalStrut(10)); getContentPane().add(p); p = new JPanel(new BorderLayout()); p.setBorder(new TitledBorder(new EtchedBorder(), "預(yù)覽")); previewLabel = new FontLabel(PREVIEW_TEXT); p.add(previewLabel, BorderLayout.CENTER); getContentPane().add(p); p = new JPanel(new FlowLayout()); JPanel p1 = new JPanel(new GridLayout(1, 2, 10, 2)); JButton btOK = new JButton("OK"); btOK.setToolTipText("Save and exit"); getRootPane().setDefaultButton(btOK); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { Closed_Option = JOptionPane.OK_OPTION; dispose(); } }; btOK.addActionListener(actionListener); p1.add(btOK); JButton btCancel = new JButton("Cancel"); btCancel.setToolTipText("Exit without save"); actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { Closed_Option = JOptionPane.CANCEL_OPTION; dispose(); } }; btCancel.addActionListener(actionListener); p1.add(btCancel); p.add(p1); getContentPane().add(p); pack(); setResizable(false); ListSelectionListener listSelectListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { updatePreview(); } }; fontNameInputList.addListSelectionListener(listSelectListener); fontSizeInputList.addListSelectionListener(listSelectListener); actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { updatePreview(); } }; boldCheckBox.addActionListener(actionListener); italicCheckBox.addActionListener(actionListener); colorComboBox.addActionListener(actionListener); underlineCheckBox.addActionListener(actionListener); strikethroughCheckBox.addActionListener(actionListener); subscriptCheckBox.addActionListener(actionListener); superscriptCheckBox.addActionListener(actionListener); } public void setAttributes(AttributeSet a) { attributes = new SimpleAttributeSet(a); String name = StyleConstants.getFontFamily(a); fontNameInputList.setSelected(name); int size = StyleConstants.getFontSize(a); fontSizeInputList.setSelectedInt(size); boldCheckBox.setSelected(StyleConstants.isBold(a)); italicCheckBox.setSelected(StyleConstants.isItalic(a)); underlineCheckBox.setSelected(StyleConstants.isUnderline(a)); strikethroughCheckBox.setSelected(StyleConstants.isStrikeThrough(a)); subscriptCheckBox.setSelected(StyleConstants.isSubscript(a)); superscriptCheckBox.setSelected(StyleConstants.isSuperscript(a)); colorComboBox.setSelectedItem(StyleConstants.getForeground(a)); updatePreview(); } public AttributeSet getAttributes() { if (attributes == null) return null; StyleConstants.setFontFamily(attributes, fontNameInputList .getSelected()); StyleConstants.setFontSize(attributes, fontSizeInputList .getSelectedInt()); StyleConstants.setBold(attributes, boldCheckBox.isSelected()); StyleConstants.setItalic(attributes, italicCheckBox.isSelected()); StyleConstants.setUnderline(attributes, underlineCheckBox.isSelected()); StyleConstants.setStrikeThrough(attributes, strikethroughCheckBox .isSelected()); StyleConstants.setSubscript(attributes, subscriptCheckBox.isSelected()); StyleConstants.setSuperscript(attributes, superscriptCheckBox .isSelected()); StyleConstants.setForeground(attributes, (Color) colorComboBox .getSelectedItem()); return attributes; } public int getOption() { return Closed_Option; } protected void updatePreview() { StringBuilder previewText = new StringBuilder(PREVIEW_TEXT); String name = fontNameInputList.getSelected(); int size = fontSizeInputList.getSelectedInt(); if (size <= 0) return; Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>(); attributes.put(FAMILY, name); attributes.put(SIZE, (float) size); // Using HTML to force JLabel manage natively unsupported attributes if (underlineCheckBox.isSelected() || strikethroughCheckBox.isSelected()) { previewText.insert(0, "<html>"); previewText.append("</html>"); } if (underlineCheckBox.isSelected()) { attributes.put(UNDERLINE, UNDERLINE_LOW_ONE_PIXEL); previewText.insert(6, "<u>"); previewText.insert(previewText.length() - 7, "</u>"); } if (strikethroughCheckBox.isSelected()) { attributes.put(STRIKETHROUGH, STRIKETHROUGH_ON); previewText.insert(6, "<strike>"); previewText.insert(previewText.length() - 7, "</strike>"); } if (boldCheckBox.isSelected()) attributes.put(WEIGHT, WEIGHT_BOLD); if (italicCheckBox.isSelected()) attributes.put(POSTURE, POSTURE_OBLIQUE); if (subscriptCheckBox.isSelected()) { attributes.put(SUPERSCRIPT, SUPERSCRIPT_SUB); } if (superscriptCheckBox.isSelected())
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -