本人不才!花了很长时间写了这个代码。不是很好,比起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"

java中如何调出字体对话框_java 字体对话框相关推荐

  1. java中的for语句格式_Java中foreach循环语句的格式可以写成(        )。

    [其它]1) 了解. 收集.整理 原研哉生平.设计理念及经典代表作 (可交) 2) google baidu 简洁的页面 丰富的变化 收集 (可交 ) [单选题]ABS 塑料通常用于 Jaguar L ...

  2. Java中的poi是什么_java poi介绍

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 结构: HSSF - 提供读写Microsoft ...

  3. java中为什么要用注解_java中的注解,真的很重要,你理解了嘛?

    这篇文章开始讲解java中的注解,在平时的开发当中我相信你或多或少的接触过注解.比如你可能都见过@override,它代表的就是一个注解.但是,为了更加清晰的去介绍注解,我还是先给出一个例子,让你能够 ...

  4. java中for break的用法_java break语句的使用方法

    在switch语中,break语句用来终止switch语句的执行.使程序 switch语句后的第一个语句 开始执行. 在Java中,可以为每个代码块加一个括号,一个代码块通常 用大括号{}括起来的一段 ...

  5. java中实现具有传递性吗_Java中volatile关键字详解,jvm内存模型,原子性、可见性、有序性...

    一.Java内存模型 想要理解volatile为什么能确保可见性,就要先理解Java中的内存模型是什么样的. Java内存模型规定了所有的变量都存储在主内存中.每条线程中还有自己的工作内存,线程的工作 ...

  6. java中的匿名类方法覆盖_Java技巧:用匿名类来实现简化程序调试

    Java技巧:用匿名类来实现简化程序调试 在Java中,匿名类(Anonymous inner classes)多用来处理事件(event handle).但其实,它们对于debug也很有帮助.本文将 ...

  7. java中用于选择按钮的语句_java程序员考试套题1

    Java练习一 一.选择题 1.在Java中,下列()方法可以把JFrame对象jFrame的布局管理器设为FlowLayout 类型. A.jFrame.setLayout(new FlowLayo ...

  8. java中集合的模型特点_Java 学习笔记(十四)集合

    集合 在开发的时候,如果需要存储多个数据,可以使用数组,不过数据是固定长度的,如果需要存储可变长度的多个数据,可以使用集合.集合提供存储空间可变的存储类型,存储的容量可以实时改变 集合分类 Colle ...

  9. java中属性文件读取案例_java相关:Spring中属性文件properties的读取与使用详解

    java相关:Spring中属性文件properties的读取与使用详解 发布于 2020-6-3| 复制链接 摘记: Spring中属性文件properties的读取与使用详解实际项目中,通常将一些 ...

  10. JAVA中console方法怎么用_Java中Console对象实例代码

    Java中Console对象实例代码 发布于 2020-12-20| 复制链接 摘记: 在JDK 6中新增了java.io.Console类,可以让您取得字节为基础的主控台装置,例如,您可以藉由Sys ...

最新文章

  1. DSP学习 -- C语言实现MySQL数据库操作
  2. react封装子组件弹框
  3. java websocket修改为同步_服务端向客户端推送消息技术之websocket的介绍
  4. 安装android sdk,后出现导出错误,提示命令行找不到解决方案
  5. 【ZOJ - 1163】The Staircases(dp)
  6. 【深度学习再突破】让计算机一眼认出“猫”:哈佛提出新高维数据分析法
  7. 选择用户-显示已选用户
  8. 一个好的函数(gcd)求最小公约数
  9. Atitit常见的标准化组织与规范数量jcp ecma iso
  10. 【推荐】前沿智能视频分析深度学习算法框架-NVIDIA DEEPSTREAM5.0 【原理介绍】
  11. oracle羊毛,预言机(Oracle)简介和撸羊毛教程— 上篇
  12. 【字符编码转换】使用iconv
  13. python:如何删除一数组中包含7和7的倍数的数
  14. php四六级报名考试源码,全国大学英语四六级考试报名官网
  15. win10更新后应用无法连接服务器,win10更新无法连接到更新服务怎么办_win10无法连接到更新服务的解决方法...
  16. 今天考了关于java认证的OCJP,特此谈谈个人java学习过程及心得
  17. 庄懂老师TA学习笔记 - 半兰伯特光照模型
  18. 【山外笔记-计算机网络·第7版】第13章:计算机网络名词缩写汇总
  19. HIS接口--LIS 与 HIS 跳转URL
  20. 【论文笔记】ECCV_2016_The Unreasonable Effectiveness of Noisy Data for Fine-Grained Recognition

热门文章

  1. 字符串匹配算法(KMP)
  2. c语言(int)x 100,【单选题】下列语句执行后,变量a、c的值分别是( ) int x=182; int a,c;c=x/100;a=x%10;...
  3. linux远程工具_【linux实操3.1】linux远程连接工具Secure的使用
  4. spark调用python_在MRS集群中使用Python3.7运行PySpark程序,调用RDD的take函数报错处理...
  5. python手机编译器可以干什么_Python是什么?Python学习用哪些编译器?
  6. 论文拒稿的评价可以有多狠?
  7. 谷歌、CMU发文:别压榨单模型了!集成+级联上分效率更高!
  8. 评测任务征集 | 全国知识图谱与语义计算大会(CCKS 2022)
  9. 百度一口气亮出NLP十年积累:完整技术布局全面披露,面向业界砸下11项七夕大礼
  10. Pytorch与tensorflow模型转换