java 字体对话框

本人不才!花了很长时间写了这个代码。不是很好,比起Windows自带的字体对话框差一点。
不过已经够用了。等将来有时间了,在进行完善。
如果有什么好的建议,可以提。本人再补。
预览效果如下图:

 

字体对话框

 

package styleDispose;

import java.awt.GraphicsEnvironment;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.ListModel;

import java.awt.Font;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.JLabel;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JButton;
import javax.swing.SwingConstants;

public class FontChooserDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 界面设计需要
	 * */
	private JPanel jPanel = null;
	private JScrollPane jScrollPane = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField fontNameText = null;
	private JTextField fontItalicText = null;
	private JTextField fontSizeText = null;
	private JList fontNameList = null;
	private JList fontItalicList = null;
	private JList fontSizeList = null;
	private JPanel jPanel2 = null;
	private JButton okButton = null;
	private JButton regitButton = null;
	private JButton cancleButton = null;
	private JScrollPane jScrollPane1 = null;
	private JScrollPane jScrollPane2 = null;
	private JScrollPane jScrollPane3 = null;
	private static JLabel fontStyle = null;
	/**
	 * 字体默认变量
	 * */
	private Font defaultFont = new Font("\u5b8b\u4f53", Font.PLAIN, 12);
	/**
	 * 返回字体变量
	 * */
	private static Font returnFont = null;
	/**
	 * Boolean 变量,判断是否正常返回,是否用户选择了字体
	 * */
	private static boolean judge = false;
	/**
	 * 以防止事件重复调用或不必要的更改,定义两个boolean变量分别
	 * 为:fontNameList和fontSizeList判断
	 * 等于true则循环调用,false则不
	 * */
	private boolean nameJuge = true;
	private boolean sizeJuge = true;
	
	
	public FontChooserDialog(){
		this(null);
	}
	public FontChooserDialog(JFrame jframe){
		this(jframe,true);
	}
	public FontChooserDialog(JFrame jframe,boolean boo){
		this(jframe,boo,null);
	}
	public FontChooserDialog(JFrame jframe,boolean boo,Font font){
		super(jframe,boo);
		initialize();
		initializeFont(font);
		this.setLocationRelativeTo(jframe);
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setContentPane(getJPanel());
        this.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
        this.setBounds(new Rectangle(0, 0, 430, 335));
        this.setTitle("字体选择对话框");
        this.addWindowListener(new WindowAdapter(){
        	public void windowClosing(WindowEvent e) {
        		judge = false;
        		closeWindow();
        	}
        });
			
	}
	public static Font showDialog(JFrame jframe,boolean boo){
		return showDialog(jframe,boo,null);
	}
	public static Font showDialog(JFrame jframe,boolean boo,Font font){
		JDialog jd = new FontChooserDialog(jframe,boo,font);
		jd.setVisible(true);
		if(judge){returnFont = fontStyle.getFont();}
		jd.dispose();
		return returnFont;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setFont(new Font("Dialog", Font.PLAIN, 12));
			jPanel.add(getJPanel1(), null);
			jPanel.add(getJPanel2(), null);
			jPanel.add(getOkButton(), null);
			jPanel.add(getRegitButton(), null);
			jPanel.add(getCancleButton(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			fontStyle = new JLabel();
			fontStyle.setText("你好!天生我才必有用!Hello World!");
			fontStyle.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontStyle.setHorizontalAlignment(SwingConstants.CENTER);
			fontStyle.setHorizontalTextPosition(SwingConstants.CENTER);
			jScrollPane = new JScrollPane();
			jScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			jScrollPane.setViewportView(fontStyle);
			jScrollPane.setBounds(new Rectangle(5, 20, 400, 60));
		}
		return jScrollPane;
	}
	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(285, 5, 120, 15));
			jLabel2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			jLabel2.setText("大小:");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(160, 5, 120, 15));
			jLabel1.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			jLabel1.setText("字型:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(5, 5, 150, 15));
			jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jLabel.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			jLabel.setText("字体:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(5, 5, 410, 175));
			jPanel1.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
			jPanel1.add(jLabel, null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getFontNameText(), null);
			jPanel1.add(getFontItalicText(), null);
			jPanel1.add(getFontSizeText(), null);
			jPanel1.add(getJScrollPane1(), null);
			jPanel1.add(getJScrollPane2(), null);
			jPanel1.add(getJScrollPane3(), null);
		}
		return jPanel1;
	}
	/**
	 * This method initializes fontNameText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFontNameText() {
		if (fontNameText == null) {
			fontNameText = new JTextField();
			fontNameText.setBounds(new Rectangle(5, 25, 150, 20));
			fontNameText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontNameText.addKeyListener(new KeyAdapter(){
				public void keyTyped(KeyEvent e) {
					String oldText = fontNameText.getText();
					String newText = "";
					if("".equals(fontNameText.getSelectedText()) && null == fontNameText.getSelectedText()){
						newText = fontNameText.getText()+e.getKeyChar();
					}else{
						newText = oldText.substring(0,fontNameText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontNameText.getSelectionEnd());
					}
					System.out.println("fontName:"+newText);
					nameJuge = false;
					fontNameList.setSelectedValue(getLateIndex(fontNameList,newText),true);
					nameJuge = true;
				}
			});
		}
		return fontNameText;
	}
	/**
	 * This method initializes fontItalicText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFontItalicText() {
		if (fontItalicText == null) {
			fontItalicText = new JTextField();
			fontItalicText.setBounds(new Rectangle(160, 25, 120, 20));
			fontItalicText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontItalicText.setEnabled(false);
		}
		return fontItalicText;
	}
	/**
	 * This method initializes fontSizeText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFontSizeText() {
		if (fontSizeText == null) {
			fontSizeText = new JTextField();
			fontSizeText.setBounds(new Rectangle(285, 25, 120, 20));
			fontSizeText.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontSizeText.setColumns(4);
			fontSizeText.addKeyListener(new KeyAdapter(){
				public void keyTyped(KeyEvent e) {
					//System.out.println("Key Code:"+(Character.getNumericValue(e.getKeyChar())==-1));
					String oldText = fontSizeText.getText();
					String newText = "";
					if("".equals(fontSizeText.getSelectedText()) && null == fontSizeText.getSelectedText()){
						newText = fontSizeText.getText()+e.getKeyChar();
					}else{
						newText = oldText.substring(0,fontSizeText.getSelectionStart())+e.getKeyChar()+oldText.substring(fontSizeText.getSelectionEnd());
					}
					//System.out.println("fontSize:"+newText);
					sizeJuge = false;
					fontSizeList.setSelectedValue(getLateIndex(fontSizeList,newText),true);
					if(newText.matches("(\\d)+")){
						fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(newText)));
					}
					sizeJuge = true;
				}
			});
			
		}
		return fontSizeText;
	}
	/**
	 * This method initializes fontNameList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getFontNameList() {
		if (fontNameList == null) {
			fontNameList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment()
					.getAvailableFontFamilyNames());
			fontNameList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontNameList
					.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
						public void valueChanged(javax.swing.event.ListSelectionEvent e) {
							if(nameJuge){fontNameText.setText(fontNameList.getSelectedValue().toString());}
							fontStyle.setFont(new Font(fontNameList.getSelectedValue().toString(),fontStyle.getFont().getStyle(),fontStyle.getFont().getSize()));
						}
					});
		}
		return fontNameList;
	}
	/**
	 * This method initializes fontItalicList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getFontItalicList() {
		if (fontItalicList == null) {
			fontItalicList = new JList(new String[]{"Plain", "Bold", "Italic","Bold Italic"});
			fontItalicList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontItalicList
					.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
						public void valueChanged(javax.swing.event.ListSelectionEvent e) {
							fontItalicText.setText(fontItalicList.getSelectedValue().toString());
							fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontItalicList.getSelectedIndex(),fontStyle.getFont().getSize()));
						}
					});
		}
		return fontItalicList;
	}
	/**
	 * This method initializes fontSizeList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getFontSizeList() {
		if (fontSizeList == null) {
			fontSizeList = new JList(new String[]{"3", "4", "5", "6", "7", "8", "9", "10",
					"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "22",
					"24", "27", "30", "34", "39", "45", "51", "60"});
			fontSizeList.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			fontSizeList
					.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
						public void valueChanged(javax.swing.event.ListSelectionEvent e) {
							if(sizeJuge){fontSizeText.setText(fontSizeList.getSelectedValue().toString());}
							fontStyle.setFont(new Font(fontStyle.getFont().getFontName(),fontStyle.getFont().getStyle(),Integer.parseInt(fontSizeList.getSelectedValue().toString())));
						}
					});
		}
		return fontSizeList;
	}
	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.setBounds(new Rectangle(3, 180, 414, 90));
			jPanel2.setBorder(BorderFactory.createTitledBorder(null, "\u6548\u679c\u9884\u89c8", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("\u5b8b\u4f53", Font.PLAIN, 12), new Color(51, 51, 51)));
			jPanel2.add(getJScrollPane(), null);
		}
		return jPanel2;
	}
	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setBounds(new Rectangle(215, 275, 60, 20));
			okButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			okButton.setText("确定");
			okButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					judge = true;
					closeWindow();
				}
			});
		}
		return okButton;
	}
	/**
	 * This method initializes regitButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRegitButton() {
		if (regitButton == null) {
			regitButton = new JButton();
			regitButton.setBounds(new Rectangle(285, 275, 60, 20));
			regitButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			regitButton.setText("重置");
			regitButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					initializeFont(null);
				}
			});
		}
		return regitButton;
	}
	/**
	 * This method initializes cancleButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancleButton() {
		if (cancleButton == null) {
			cancleButton = new JButton();
			cancleButton.setBounds(new Rectangle(355, 275, 60, 20));
			cancleButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 12));
			cancleButton.setText("取消");
			cancleButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					judge = false;
					closeWindow();
				}
			});
		}
		return cancleButton;
	}
	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(5, 50, 150, 120));
			jScrollPane1.setViewportView(getFontNameList());
		}
		return jScrollPane1;
	}
	/**
	 * This method initializes jScrollPane2	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane2() {
		if (jScrollPane2 == null) {
			jScrollPane2 = new JScrollPane();
			jScrollPane2.setBounds(new Rectangle(160, 50, 120, 120));
			jScrollPane2.setViewportView(getFontItalicList());
		}
		return jScrollPane2;
	}
	/**
	 * This method initializes jScrollPane3	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane3() {
		if (jScrollPane3 == null) {
			jScrollPane3 = new JScrollPane();
			jScrollPane3.setBounds(new Rectangle(285, 50, 120, 120));
			jScrollPane3.setViewportView(getFontSizeList());
		}
		return jScrollPane3;
	}
	
	/**
	 * 默认的字体初始化方法
	 * */
	private void initializeFont(Font font){
		if(font!=null){
			defaultFont = font;
			fontStyle.setFont(defaultFont);
		}
		fontStyle.setFont(defaultFont);
		fontNameList.setSelectedValue(defaultFont.getFontName(), true);
		fontSizeList.setSelectedValue(new Integer(defaultFont.getSize()).toString(), true);
		fontItalicList.setSelectedIndex(defaultFont.getStyle());
	}
	/**
	 * 判断里给定的值最近的索引
	 * */
	private Object getLateIndex(JList jlist,String str){
		ListModel list = jlist.getModel();
		if(str.matches("(\\d)+")){
			for(int i = list.getSize()-1;i>=0;i--){
				if(Integer.parseInt(list.getElementAt(i).toString())<=Integer.parseInt(str)){
					return list.getElementAt(i);
				}
			}
		}else {
			for(int i = list.getSize()-2;i>=0;i--){
				if(str.compareToIgnoreCase(list.getElementAt(i).toString())==0){
					return list.getElementAt(i);
				}else if(str.compareToIgnoreCase(list.getElementAt(i).toString())>0){
					return list.getElementAt(i+1);
				}
			}
		}
		return list.getElementAt(0);
	}
	/**
	 * 窗体关闭方法!
	 * */
	private void closeWindow(){
		this.setVisible(false);
	}
}  //  @jve:decl-index=0:visual-constraint="91,34"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值