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

📄 fontselector.java

📁 更方便的SWING
💻 JAVA
字号:
/*
 * PSwing Utilities -- Nifty Swing Widgets
 * Copyright (C) 2002  Pallas Technology
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Pallas Technology
 * 1170 HOWELL MILL RD NW
 * SUITE 306
 * ATLANTA GEORGIA 30318
 * 
 * PHONE 404.983.0623
 * EMAIL info@pallastechnology.com
 * 
 * www.pallastechnology.com
 **************************************************************************
 * $Archive: SwingTools$
 * $FileName: FontSelector.java$
 * $FileID: 34$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 8/12/03 12:13 AM$
 * $VerID: 99$
 * $Comment: Use PIntField for size field.$
 **************************************************************************/
package com.pallas.swing.font;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.pallas.swing.PIntField;
import com.pallas.swing.pcombobox.PComboBox;

/**
 * Title:   $FileName: FontSelector.java$
 * @version $VerNum: 3$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: $<br>
 * $KeyWordsOff: $<br><br>
 */
public class FontSelector extends JPanel {
  
  private Font font = null;
  private JButton browseButton = null;
  private JLabel display = null;
  private PComboBox fontName = null;
  private PIntField fontSize = null;
  private JCheckBox fontBold = null;
  private JCheckBox fontItalic = null;
  
  public FontSelector(){
    initializeFontSelector();
  }
  
  public FontSelector(Font font){
    this.font = font;
    initializeFontSelector();
  }
  
  private void initializeFontSelector(){
    fontName = buildFontListing();
    selectFontName(fontName);
    fontSize = new PIntField(getDefaultFontSize());
    fontSize.setColumns(4);
    fontBold = new JCheckBox("Bold");
    setDefaultBoldStatus(fontBold);
    fontItalic = new JCheckBox("Italic");
    setDefaultBoldStatus(fontItalic);

    add(fontName);
    add(new JLabel(" Size: "));
    add(fontSize);
    add(fontBold);
    add(fontItalic);
  }

  public Font getSelectedFont(){
    String name = fontName.getSelectedItem().toString();
    int size = fontSize.getInt();
    boolean bold = fontBold.isSelected();
    boolean ital = fontItalic.isSelected();
    Font f = new Font(name, (bold ? Font.BOLD : 0) +
                            (ital ? Font.ITALIC : 0), size);
    return f;
  }

  private void setDefaultBoldStatus(JCheckBox box){
    if (font != null){
      if (font.getStyle() == Font.BOLD ||
          font.getStyle() == Font.BOLD + Font.ITALIC){
        box.setSelected(true);
      }
    }
  }

  private void setDefaultItalicStatus(JCheckBox box){
    if (font != null){
      if (font.getStyle() == Font.ITALIC ||
          font.getStyle() == Font.BOLD + Font.ITALIC){
        box.setSelected(true);
      }
    }
  }
  
  private int getDefaultFontSize(){
    int size = 12;
    if (font != null){
      size = font.getSize();
    }
    return size;
  }

  private void selectFontName(PComboBox combo){
    if(font != null){
      combo.setSelectionByDisplay(font.getName());
    }
    else{
      combo.setSelectedIndex(1);
    }
  }

//  private JLabel buildDisplay(Font font){
//    String name = font.getName();
//    int size = font.getSize();
//  }
  
  private PComboBox buildFontListing(){
    Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    Vector allFontNames = new Vector();
    for(int i = 0; i < allFonts.length; i++){
      allFontNames.add(allFonts[i].getName());
    }
    PComboBox fontCombo = new PComboBox(allFontNames);
    fontCombo.buildIndex();
    return fontCombo;
  }
  
}

⌨️ 快捷键说明

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