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

📄 fontdialog.java

📁 java实现的文本编辑器
💻 JAVA
字号:
package MulitePageEditor;

import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;

/**
 * <p>
 * Title: FontDialog
 * </p>
 * 
 * <p>
 * Description: 用于选择一种字体
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006-2008
 * </p>
 * 
 * <p>
 * Company: 雨轩工作室
 * </p>
 * 
 * @author 风中过客
 * @version V1.0
 */
public class FontDialog extends JDialog
{
	// JPanel panel1 = new JPanel();
	JComboBox cmbFontSize = new JComboBox();

	JComboBox cmbFontName = new JComboBox();

	JTextArea txtSample = new JTextArea();

	JButton btnOk = new JButton();

	JButton btnCancel = new JButton();

	// PaneLayout paneLayout1 = new PaneLayout();
	XYLayout xYLayout1 = new XYLayout();

	private Font selectedfont = new Font("宋体", Font.PLAIN, 12);

	private String simpleText = "ABCabc风中过客";

	private DialogResult dialggResult = DialogResult.Cancel;

	private GraphicsEnvironment graphicsEnvironment;

	// ButtonGroup buttonGroup1 = new ButtonGroup();

	public enum DialogResult
	{
		Ok, Cancel,
	}

	public FontDialog(Frame owner, String title, Font oldFont,boolean modal)
	{
		super(owner, title, modal);
		try
		{
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			jbInit();
			initEvent();
			initCombox();
			setOldFont(oldFont);
			txtSample.setText(simpleText);
			txtSample.setEditable(false);
			txtSample.setEnabled(false);
			//this.setLocationByPlatform(true);
			this.setLocationRelativeTo(owner);
			pack();
		} catch (Exception exception)
		{
			exception.printStackTrace();
		}
	}

	public FontDialog()
	{
		this(new Frame(), "请选择一种字体", new Font("宋体", Font.PLAIN, 12),true);
	}

	private void jbInit() throws Exception
	{
		// panel1.setLayout(paneLayout1);
		btnOk.setMnemonic('O');
		btnOk.setText("确定(O)");
		btnCancel.setMnemonic('C');
		btnCancel.setSelectedIcon(null);
		btnCancel.setText("取消(C)");
		cmbFontName.setMinimumSize(new Dimension(40, 40));
		cmbFontName.setPreferredSize(new Dimension(120, 27));
		cmbFontName.setMaximumRowCount(10);
		cmbFontSize.setPreferredSize(new Dimension(120, 27));
		cmbFontSize.setMaximumRowCount(10);
		this.getContentPane().setLayout(xYLayout1);
		xYLayout1.setWidth(389);
		xYLayout1.setHeight(202);
		// this.getContentPane().add(panel1, new XYConstraints(39, 13, -1, -1));
		this.getContentPane().add(cmbFontSize,
				new XYConstraints(225, 7, 150, -1));
		this.getContentPane().add(cmbFontName,
				new XYConstraints(28, 7, 173, -1));
		this.getContentPane()
				.add(txtSample, new XYConstraints(24, 59, 359, 74));
		this.getContentPane().add(btnCancel,
				new XYConstraints(307, 156, 78, -1));
		this.getContentPane().add(btnOk, new XYConstraints(205, 157, 88, -1));
	}

	private void initCombox()
	{
		fillFontNames();
		//setAvailableFonts(getAvailableFonts());
		fillFontSizes(getFontSizes());
	}

	public void fillFontSizes(String[] fontSizes)
	{
		cmbFontSize.setModel(new DefaultComboBoxModel(fontSizes));
	}
	
/*	public void setAvailableFonts(Font[] availableFonts) {
		cmbFontName.setModel(new DefaultComboBoxModel(availableFonts));
	  }

	  public Font[] getAvailableFonts() {
	    Font[] items = new Font[cmbFontName.getItemCount()];
	    for (int itemNo = 0; itemNo < items.length; itemNo++) {
	      items[itemNo] = (Font) cmbFontName.getItemAt(itemNo);
	    }
	    return items;
	  }*/

	public String[] getFontSizes()
	{
		String[] items = {"8.0",
				          "10.0",
				          "12.0",
				          "14.0",
				          "16.0",
				          "18.0",
				          "24.0",
				          "36.0",
				          "48.0"};
		
		return items;
	}

	private void fillFontNames()
	{
		if (graphicsEnvironment == null)
		{
			graphicsEnvironment = GraphicsEnvironment
					.getLocalGraphicsEnvironment();
		}
		Font[] fonts=graphicsEnvironment.getAllFonts();
		String[] objs=new String[fonts.length];
		for(int i=0;i<fonts.length;i++)
		{
			objs[i]=fonts[i].getFamily();
		}
		cmbFontName.setModel(new DefaultComboBoxModel(objs));
	}

	private void initEvent()
	{
		this.btnOk.addActionListener(action);
		this.btnCancel.addActionListener(action);
		this.cmbFontName.addItemListener(cmbaction);
		this.cmbFontSize.addItemListener(cmbaction);
	}

	private String getFontFimaly()
	{
		/*int count=cmbFontName.getItemCount();
		for(int i=0;i<count;i++)
		{
			retur
		}*/
		return cmbFontName.getSelectedItem().toString();
	}
	
	public int getFontSize()
	{
		// return ((Integer)cmbFontSize.getSelectedItem()).intValue();
		int index = cmbFontSize.getSelectedIndex();
		int size=12;
		switch (index)
		{
		case 0:
			size=8;
			break;
		case 1:
			size=10;
			break;
		case 2:
			size=12;
			break;
		case 3:
			size=14;
			break;
		case 4:
		    size=16;
			break;
		case 5:
			size=18;
			break;
		case 6:
			size=24;
			break;
		case 7:
			size=36;
			break;
		case 8:
			size=48;
			break;
		}
		return size;
		// java.lang.Integer
	}
	
	ItemListener cmbaction = new ItemListener()
	{
		public void itemStateChanged(ItemEvent event)
		{
			Font ff=new Font(getFontFimaly(),Font.PLAIN,getFontSize());
			txtSample.setFont(ff);
		}
	};
	
	ActionListener action = new ActionListener()
	{
		public void actionPerformed(ActionEvent event)
		{
			if (event.getSource().equals(btnOk))
			{
				dialggResult = DialogResult.Ok;
				selectedfont=new Font(getFontFimaly(),Font.PLAIN,getFontSize());				
			} else if (event.getSource().equals(btnCancel))
			{
				dialggResult = DialogResult.Cancel;
			}
			Hide(); /*不能在该接口中访问this的好多方法,只好这样了*/
		}
	};

	private void Hide()
	{
		this.setVisible(false);
	}

	private void setOldFont(Font font)
	{
         String str =font.getFamily();
         int items=cmbFontName.getItemCount();
         for(int i=0;i<items;i++)
         {
        	 if(str.equals(cmbFontName.getItemAt(i).toString()))
        	 {
        		 cmbFontName.setSelectedIndex(i);  //设置选中项
        		 break;
        	 }
         }
         String size=font.getSize()+".0";
         int count=cmbFontSize.getItemCount();
         for(int i=0;i<count;i++)
         {
        	 if(size.equals(cmbFontSize.getItemAt(i).toString()))
        	 {
        		// System.out.println(cmbFontSize.getItemAt(i).toString());
        		 cmbFontSize.setSelectedIndex(i);
        		 break;
        	 }
         }
	}

	public Font getSelectedFont()
	{
		return selectedfont;
	}

	public void setSimpleText(String text)
	{
		simpleText = text;
	}

	public DialogResult getDialogResult()
	{
		return dialggResult;
	}

	public void showDialog()
	{
		this.setSize(400, 220);
		this.setVisible(true);
	}

	public void Dispose()
	{
		this.dispose();
	}
}

⌨️ 快捷键说明

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