⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fontchooser.java

📁 JavaExplorer是一个独立于平台的浏览器
💻 JAVA
字号:
/**  * File and FTP Explorer  * Copyright 2003  * BOESCH Vincent  *  * This program is free software; you can redistribute it and/or  * modify it under the terms of the GNU General Public License  * as published by the Free Software Foundation; either version 2  * of the License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */package javaexplorer.gui.dialog;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;public class FontChooser extends JDialog implements ActionListener {    static Font[] tb_f;    static {        tb_f = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();    }    JComboBox fontName;    JCheckBox fontBold;    JCheckBox fontItalic;    JTextField fontSize;    JLabel previewLabel;    SimpleAttributeSet attributes;    Font newFont;    Color newColor;    public FontChooser(Frame parent, Font oldFont) {        super(parent, "Font Chooser", true);        setSize(500, 160);        attributes = new SimpleAttributeSet();        // make sure that any way they cancel the window does the right thing        addWindowListener(new WindowAdapter() {                public void windowClosing(WindowEvent e) {                    closeAndCancel();                }            });        // Start the long process of setting up our interface        Container c = getContentPane();        int indexSelected = 1;        JPanel fontPanel = new JPanel();        fontName = new JComboBox();        for (int i = 0; i < tb_f.length; i++) {            fontName.addItem(tb_f[i].getName());            if ((oldFont != null) &&                    tb_f[i].getName().equals(oldFont.getName())) {                indexSelected = i;            }        }        fontName.setSelectedIndex(indexSelected);        fontName.addActionListener(this);        fontSize = new JTextField((oldFont == null) ? "12"                                                    : ("" + oldFont.getSize()),                4);        fontSize.setHorizontalAlignment(SwingConstants.RIGHT);        fontSize.addActionListener(this);        fontBold = new JCheckBox("Bold");        fontBold.setSelected((oldFont == null) ? false : oldFont.isBold());        fontBold.addActionListener(this);        fontItalic = new JCheckBox("Italic");        fontItalic.setSelected((oldFont == null) ? false : oldFont.isItalic());        fontItalic.addActionListener(this);        fontPanel.add(fontName);        fontPanel.add(new JLabel(" Size: "));        fontPanel.add(fontSize);        fontPanel.add(fontBold);        fontPanel.add(fontItalic);        c.add(fontPanel, BorderLayout.NORTH);        JPanel previewPanel = new JPanel(new BorderLayout());        previewLabel = new JLabel("Here's a sample of this font.");        previewPanel.add(previewLabel, BorderLayout.CENTER);        previewLabel.setFont(oldFont);        // Add in the Ok and Cancel buttons for our dialog box        JButton okButton = new JButton("Ok");        okButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    closeAndSave();                }            });        JButton cancelButton = new JButton("Cancel");        cancelButton.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent ae) {                    closeAndCancel();                }            });        JPanel controlPanel = new JPanel();        controlPanel.add(okButton);        controlPanel.add(cancelButton);        previewPanel.add(controlPanel, BorderLayout.SOUTH);        // Give the preview label room to grow.        previewPanel.setMinimumSize(new Dimension(100, 100));        previewPanel.setPreferredSize(new Dimension(100, 100));        c.add(previewPanel, BorderLayout.SOUTH);    }    // Ok, something in the font changed, so figure that out and make a    // new font for the preview label    public void actionPerformed(ActionEvent ae) {        // Check the name of the font        if (!StyleConstants.getFontFamily(attributes).equals(fontName.getSelectedItem())) {            StyleConstants.setFontFamily(attributes,                (String) fontName.getSelectedItem());        }        // Check the font size (no error checking yet)        if (StyleConstants.getFontSize(attributes) != Integer.parseInt(                    fontSize.getText())) {            StyleConstants.setFontSize(attributes,                Integer.parseInt(fontSize.getText()));        }        // Check to see if the font should be bold        if (StyleConstants.isBold(attributes) != fontBold.isSelected()) {            StyleConstants.setBold(attributes, fontBold.isSelected());        }        // Check to see if the font should be italic        if (StyleConstants.isItalic(attributes) != fontItalic.isSelected()) {            StyleConstants.setItalic(attributes, fontItalic.isSelected());        }        // and update our preview label        updatePreviewFont();    }    // Get the appropriate font from our attributes object and update    // the preview label    protected void updatePreviewFont() {        String name = StyleConstants.getFontFamily(attributes);        boolean bold = StyleConstants.isBold(attributes);        boolean ital = StyleConstants.isItalic(attributes);        int size = StyleConstants.getFontSize(attributes);        //Bold and italic don't work properly in beta 4.        Font f = new Font(name,                (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);        previewLabel.setFont(f);    }    // Get the appropriate color from our chooser and update previewLabel    public Font getNewFont() {        return newFont;    }    public void closeAndSave() {        // save font & color information        newFont = previewLabel.getFont();        // and then close the window        setVisible(false);    }    public void closeAndCancel() {        // erase any font information and then close the window        newFont = null;        setVisible(false);    }    /*  public static void main( String argv[] ){        JFrame jf = new JFrame();        jf.setBounds(100, 100, 300, 300);        jf.setVisible(true);        FontChooser fc = new FontChooser( jf, null );        fc.setVisible(true);      }    */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -