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

📄 fontdialog.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 11/14/2003
 *
 * FontDialog.java - A modal dialog box letting the user choose a font and
 * optionally a font color.
 * Copyright (C) 2003 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * 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 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 org.fife.ui;

import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

import org.fife.RListSelectionModel;
import org.fife.RUtilities;


/**
 * A dialog box that allows the user to choose from all fonts available to
 * the application on the system, as well as choose a font color.
 *
 * @author Robert Futrell
 *
 * @version 1.0
 */
public class FontDialog extends JDialog implements ActionListener,
										ListSelectionListener,
										Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2089267194781772827L;

	private String[] fontFamilies;	// All fonts available to the program.
	private JList fontList;			// List displaying all available fonts.
	private JList fontSizeList;		// List displaying font sizes.
	private JLabel sampleTextLabel;	// A "sample" of the selected font.

	private JButton okButton;
	private JButton cancelButton;

	private JPanel fontFormatPanel;
	private JPanel samplePanel;

	private JCheckBox boldCheckBox;		// Sets bold property.
	private JCheckBox italicCheckBox;		// Sets italics property.
	private JCheckBox underlineCheckBox;	// Sets underline property (NOT CURRENTLY WORKING).

	private int properties = 0;			// Bit flag for bold/italics.
	private int size = 12;				// Point size of font.

	private Font selectedFont;			// The font they end up selecting (or null if they cancel).
	private Color selectedColor;			// The color they wish to use for the font.

	private JLabel fontChooserLabel;		// Label for font list.
	private String styleLabel;			// Label for style list.
	private JLabel sizeLabel;			// Label for size list.
	private String sampleLabel;			// Label for sample panel.


/*****************************************************************************/


	/**
	 * Creates a new font dialog with <code>null</code> as owner, with initial
	 * selection of a 10-point monospaced font.  Underline and font color
	 * options will not be available for selection.
	 */
	public FontDialog() {
		this(null, null, new Font("Monospaced", Font.PLAIN, 10),
			Color.BLACK, false, false);
	}


/*****************************************************************************/


	/**
	 * Creates a new font dialog from which the user can choose the font
	 * property "underline" and the font color.
	 *
	 * @param owner The window for which you are picking a font.
	 * @param title The title of this dialog (e.g., "Font").
	 * @param initialSelection The font that this dialog initially has
	 *        selected.  A <code>null</code> value defaults to
	 *        <code>owner</code>'s font.
	 * @param initialColor The color to use for the font initially.  A
	 *        <code>null</code> value defaults to black.
	 */
	public FontDialog(Frame owner, String title, Font initialSelection,
					Color initialColor) {
		this(owner, title, initialSelection, initialColor, true, true);
	}

/*****************************************************************************/


	/**
	 * Creates a new font dialog.
	 *
	 * @param owner The window for which you are picking a font.
	 * @param title The title of this dialog (e.g., "Font").
	 * @param initialSelection The font that this dialog initially has
	 *        selected.  A <code>null</code> value defaults to
	 *        <code>owner</code>'s font.
	 * @param initialColor The color to use for the font initially.  A
	 *        <code>null</code> value defaults to black.
	 * @param underlineSelectable Whether or not the user can select
	 *        "underline" as a property for the font being chosen.
	 * @param colorSelectable Whether or not the user can change the font's
	 *        color from here.
	 */
	public FontDialog(Frame owner, String title, Font initialSelection,
					Color initialColor, boolean underlineSelectable,
					boolean colorSelectable) {

		// Call JDialog's constructor.
		super(owner);

		ResourceBundle bundle = ResourceBundle.getBundle("org.fife.ui.UI");

		if (title==null)
			title = bundle.getString("FontDialog.Title");
		setTitle(title);

		// Get available fonts from the system.
		fontFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment().
										getAvailableFontFamilyNames();

		// Create a panel for choosing the font.
		JPanel fontChooserPanel = new JPanel();
		DefaultListModel listModel = new DefaultListModel();
		for (int i=0; i<fontFamilies.length; i++)
			listModel.addElement(fontFamilies[i]);
		fontList = new JList(listModel);
		fontList.setSelectionModel(new RListSelectionModel());
		fontList.getSelectionModel().addListSelectionListener(this);
		JScrollPane fontListScrollPane = new RScrollPane(fontList);
		fontChooserPanel.setLayout(new BoxLayout(fontChooserPanel,
												BoxLayout.Y_AXIS));
		fontChooserLabel = RUtilities.createLabel(bundle, "FontLabel",
											"FontLabelMnemonic");
		fontChooserPanel.add(fontChooserLabel);
		fontChooserPanel.add(fontListScrollPane);
		fontChooserLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		fontListScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);

		// Create a panel for choosing the font's point size.
		JPanel fontSizePanel = new JPanel();
		listModel = new DefaultListModel();
		for (int i=2; i<=40; i++)
			listModel.addElement(new Integer(i).toString());
		fontSizeList = new JList(listModel);
		fontSizeList.setSelectionModel(new RListSelectionModel());
		fontSizeList.getSelectionModel().addListSelectionListener(this);
		JScrollPane fontSizeListScrollPane = new RScrollPane(fontSizeList);
		fontSizePanel.setLayout(new BoxLayout(fontSizePanel,
												BoxLayout.Y_AXIS));
		sizeLabel = RUtilities.createLabel(bundle, "SizeLabel",
									"SizeLabelMnemonic");
		fontSizePanel.add(sizeLabel);
		fontSizePanel.add(fontSizeListScrollPane);
		sizeLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		fontSizeListScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);

		// Create a panel for bold/italic/underline.
		boldCheckBox = RUtilities.createCheckBox(bundle, "Bold",
											"BoldMnemonic");
		italicCheckBox = RUtilities.createCheckBox(bundle, "Italic",
											"ItalicMnemonic");
		underlineCheckBox = RUtilities.createCheckBox(bundle, "Underline",
											"UnderlineMnemonic");
		boldCheckBox.setActionCommand("Bold");
		italicCheckBox.setActionCommand("Italic");
		underlineCheckBox.setActionCommand("Underline");
		boldCheckBox.addActionListener(this);
		italicCheckBox.addActionListener(this);
		underlineCheckBox.addActionListener(this);
		if (underlineSelectable==false)
			underlineCheckBox.setEnabled(false);
		fontFormatPanel = new JPanel();
		BoxLayout box = new BoxLayout(fontFormatPanel, BoxLayout.Y_AXIS);
		fontFormatPanel.setLayout(box);
		fontFormatPanel.add(boldCheckBox);
		fontFormatPanel.add(italicCheckBox);
		fontFormatPanel.add(underlineCheckBox);
		styleLabel = bundle.getString("FontStyle");
		setFontStyleLabel(styleLabel);

		// Create a panel for the OK and Cancel buttons.
		okButton = RUtilities.createRButton(bundle, "OK", "OKButtonMnemonic");
		okButton.setActionCommand("OK");
		okButton.addActionListener(this);
		cancelButton = RUtilities.createRButton(bundle, "Cancel",
											"CancelButtonMnemonic");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(this);
		JButton colorButton = RUtilities.createRButton(bundle, "Color",
											"ColorButtonMnemonic");
		colorButton.setActionCommand("Color");
		colorButton.addActionListener(this);
		if (colorSelectable==false)
			colorButton.setVisible(false);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(4,1, 0,5));
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		buttonPanel.add(Box.createRigidArea(new Dimension(5,5)));
		buttonPanel.add(colorButton);

		// Create a panel containing the previous panels.
		JPanel topPanel = new JPanel();
		topPanel.add(fontChooserPanel);
		topPanel.add(fontFormatPanel);
		topPanel.add(fontSizePanel);
		topPanel.add(buttonPanel);

		topPanel.setBorder(UIUtilities.getEmpty5Border());

		// Create a panel that shows a "sample" of the selected font.
		samplePanel = new JPanel();
		sampleLabel = bundle.getString("Sample");
		setFontSampleLabel(sampleLabel);
		sampleTextLabel = new JLabel(bundle.getString("SampleText"));
		sampleTextLabel.setHorizontalAlignment(SwingConstants.CENTER);
		sampleTextLabel.setPreferredSize(new Dimension(250,80));
		samplePanel.add(sampleTextLabel);

		// Put it all together.
		if (initialSelection==null)
			initialSelection = owner!=null ? owner.getFont() :
								new Font("monospaced", Font.PLAIN, 13);
		if (initialSelection.isBold())
			properties |= Font.BOLD;
		if (initialSelection.isItalic())
			properties |= Font.ITALIC;
		//if (initialSelection.isUnderline())
		//	properties |= Font.UNDERLINE;
		setSelectedFont(initialSelection, initialColor);
		getRootPane().setDefaultButton(okButton);
		getContentPane().setLayout(new BoxLayout(getContentPane(),
												BoxLayout.Y_AXIS));
		getContentPane().add(topPanel);
		getContentPane().add(samplePanel);
		pack();

		setResizable(false);
		setModal(true);


	}


/*****************************************************************************/


	// What to do when some property of the prospective new font changes.
	public void actionPerformed(ActionEvent e) {

		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Bold")) {
			properties ^= Font.BOLD;
			Font newFont = sampleTextLabel.getFont().deriveFont(properties, size);
			sampleTextLabel.setFont(newFont);
		}

		else if (actionCommand.equals("Italic")) {
			properties ^= Font.ITALIC;
			Font newFont = sampleTextLabel.getFont().deriveFont(properties, size);
			sampleTextLabel.setFont(newFont);
		}

		else if (actionCommand.equals("Underline")) {
			//properties ^= Font.CENTER_BASELINE;
			//Font newFont = sampleLabel.getFont().deriveFont(properties, size);
			//sampleTextLabel.setFont(newFont);
			if (getUnderlineSelected()) {
				sampleTextLabel.setText("<html><u>" + sampleTextLabel.getText() + "</u>");
			}

⌨️ 快捷键说明

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