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

📄 fontdialog.java

📁 一个简易的java画图软件
💻 JAVA
字号:
package app.pane;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 字体对话框
 * 
 * @author Thihy
 * 
 */
public class FontDialog extends JDialog {
	final JTextField textField;
	final JTextList listFont;
	final JTextList listStyle;
	final JTextList listSize;
	final JComboBox comboBoxColor;
	final JLabel labelExample;
	private Font theFont;
	private String tempText;
	private Font tempFont;
	private Color theColor;
	private Color tempColor;

	private String text;

	private final static int Y0 = 30, Y1 = 58, Y2 = 80, Y3 = 230;

	private final static boolean DEBUG = false;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	/*
	 * public static void main(String args[]) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { FontDialog dialog = new
	 * FontDialog(new Font("微软雅黑",Font.PLAIN,16),null,"sdfasdf");
	 * dialog.addWindowListener(new WindowAdapter() { public void
	 * windowClosing(WindowEvent e) { System.exit(0); } });
	 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 * }); }
	 */

	/**
	 * Create the dialog
	 */
	public FontDialog(Font font, Color color, String str) {
		super();
		setModalityType(ModalityType.APPLICATION_MODAL);
		setName("fontDialog");
		getContentPane().setLayout(null);
		setResizable(false);
		setTitle("文字属性");
		setBounds(100, 100, 461, 326);

		tempFont = theFont = (font == null) ? new Font("微软雅黑", Font.PLAIN, 16)
				: font;
		tempColor = theColor = (color == null) ? Color.BLACK : color;
		tempText = text = (str == null) ? "" : str;

		// System.out.println(tempColor);

		textField = new JTextField(this.text);
		textField.setBounds(10, Y0, 437, 22);
		textField.setText(tempText);
		getContentPane().add(textField);

		textField.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent e) {
				tempText = textField.getText();
				updateExampleText();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				tempText = textField.getText();
				updateExampleText();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				tempText = textField.getText();
				updateExampleText();
			}
		});

		listFont = new JTextList();

		// 获取所有字体
		final String[] fontList = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
		listFont.setListData(fontList);
		listFont.setBounds(10, Y2, 150, 136);
		getContentPane().add(listFont);

		listStyle = new JTextList();

		final int[] styleList = { Font.PLAIN, Font.BOLD, Font.ITALIC,
				Font.BOLD + Font.ITALIC };
		String[] styleNameList = { "常规", "粗体", "斜体", "粗斜体" };

		listStyle.setBounds(166, Y2, 124, 136);
		listStyle.disableText();
		listStyle.setListData(new String[] { "常规", "粗体", "斜体", "粗斜体" });
		getContentPane().add(listStyle);

		listSize = new JTextList();

		String[] sizeNameList = { "8", "9", "10", "11", "12", "14", "16", "18",
				"20", "22", "24", "26", "28", "36", "48", "72" };

		listSize.setBounds(296, Y2, 60, 136);
		listSize.setListData(sizeNameList);
		getContentPane().add(listSize);

		final JButton buttonOk = new JButton();
		buttonOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(final MouseEvent e) {
				if (DEBUG)
					System.out.println(e);
				theFont = new Font(tempFont.getFamily(), tempFont.getStyle(),
						Integer.parseInt(listSize.getTextField().getText()));
				theColor = tempColor;
				text = tempText;
				setVisible(false);
			}
		});
		buttonOk.setText("确定");
		buttonOk.setBounds(362, Y1, 84, 23);
		getContentPane().add(buttonOk);

		final JButton buttonCancel = new JButton();
		buttonCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(final MouseEvent e) {
				if (DEBUG)
					System.out.println(e);
				setVisible(false);
			}
		});
		buttonCancel.setText("取消");
		buttonCancel.setBounds(362, Y1 + 29, 84, 23);
		getContentPane().add(buttonCancel);

		final JLabel labelText = new JLabel();
		labelText.setText("文本内容:");
		labelText.setBounds(10, Y0 - 25, 66, 18);
		getContentPane().add(labelText);

		final JLabel labelFont = new JLabel();
		labelFont.setText("字体:");
		labelFont.setBounds(10, Y1, 66, 18);
		getContentPane().add(labelFont);

		final JLabel labelStyle = new JLabel();
		labelStyle.setText("字形:");
		labelStyle.setBounds(166, Y1, 66, 18);
		getContentPane().add(labelStyle);

		final JLabel labelSize = new JLabel();
		labelSize.setText("大小:");
		labelSize.setBounds(296, Y1, 66, 18);
		getContentPane().add(labelSize);

		labelExample = new JLabel();
		labelExample.setHorizontalTextPosition(SwingConstants.CENTER);
		labelExample.setHorizontalAlignment(SwingConstants.LEFT);
		labelExample.setBorder(new TitledBorder(null, "示例",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		labelExample.setText(text);
		labelExample.setBounds(166, Y3, 280, 50);
		getContentPane().add(labelExample);

		final JLabel labelColor = new JLabel();
		labelColor.setText("颜色");
		labelColor.setBounds(10, Y3, 66, 18);
		getContentPane().add(labelColor);

		comboBoxColor = new JComboBox();
		// 一些颜色
		final String[] colorNameList = { "黑色", "蓝色", "蓝绿色", "浅灰色", "灰色", "深灰色",
				"绿色", "紫红色", "橙色", "粉红色", "红色", "黄色", "其他颜色..." };
		final Color[] colorList = { Color.BLACK, Color.BLUE, Color.CYAN,
				Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.GREEN,
				Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED,
				Color.YELLOW };

		comboBoxColor.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				int size = comboBoxColor.getModel().getSize();
				if (size <= 0)
					return;
				int selectedIndex = comboBoxColor.getSelectedIndex();
				if (selectedIndex >= colorList.length) {
					tempColor = JColorChooser.showDialog(null, "选择颜色",
							tempColor);
					comboBoxColor.setSelectedIndex(0);
				} else {
					tempColor = colorList[selectedIndex];
				}
				updateExampleText();
			}
		});

		comboBoxColor.setModel(new DefaultComboBoxModel(colorNameList));
		comboBoxColor.setBounds(10, Y3 + 24, 150, 23);
		getContentPane().add(comboBoxColor);

		listFont.getList().addListSelectionListener(
				new ListSelectionListener() {
					public void valueChanged(final ListSelectionEvent e) {
						int selectedIndex = listFont.getList()
								.getSelectedIndex();
						if (selectedIndex < 0)
							return;
						tempFont = new Font((String) (listFont.getList()
								.getSelectedValue()), tempFont.getStyle(),
								tempFont.getSize());
						updateExampleText();
					}
				});

		listStyle.getList().addListSelectionListener(
				new ListSelectionListener() {
					public void valueChanged(final ListSelectionEvent e) {
						int selectedIndex = listStyle.getList()
								.getSelectedIndex();
						if (selectedIndex < 0)
							return;
						tempFont = new Font(tempFont.getFamily(),
								styleList[selectedIndex], tempFont.getSize());
						updateExampleText();
					}
				});

		listSize.getTextField().getDocument().addDocumentListener(
				new DocumentListener() {

					@Override
					public void removeUpdate(DocumentEvent e) {
						updateExLabel();
					}

					@Override
					public void insertUpdate(DocumentEvent e) {
						updateExLabel();
					}

					@Override
					public void changedUpdate(DocumentEvent e) {
						updateExLabel();
					}

					public void updateExLabel() {
						listSize.getList()
								.getSelectedIndex();
						try {
							tempFont = new Font(tempFont.getFamily(), tempFont
									.getStyle(), Integer.parseInt(listSize
									.getTextField().getText()));
							updateExampleText();
						} catch (Exception ex) {

						}
					}
				});

		/*
		 * if (font != null) {
		 * listFont.getTextField().setText(font.getFamily());
		 * listStyle.getTextField().setText(styleNameList[font.getStyle()]);
		 * listSize
		 * .getTextField().setText(((Integer)(font.getSize())).toString()); }
		 */
		if (font != null) {
			listFont.selectText(font.getFamily());
			listStyle.selectText(styleNameList[font.getStyle()]);
			listSize.selectText(((Integer) (font.getSize())).toString());
		}
	}

	private void updateExampleText() {
		labelExample.setText(tempText);
		labelExample.setFont(tempFont);
		labelExample.setForeground(tempColor);
	}

	public Font getChoosedFont() {
		return theFont;
	}

	public Color getChoosedColor() {
		return theColor;
	}

	public String getText() {
		return text;
	}

}

⌨️ 快捷键说明

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