`

Java 调用系统的字体和颜色【转】

    博客分类:
  • JAVA
阅读更多
Java 调用系统的字体和颜色

博客分类: JASE
JavaSwing设计模式F#
  下面的这个程序是项目里面的一个对话框,可以直接改成窗体,将JDailog改为JFrame就可以了
   在设置字体的时候,先选择好字体的那三个参数,在点击确定
   本程序的确定,不能对选中的部分内容进行设置,而是设置全部的内容
package cn.com.peixunban.view.dialog.toolDialog;
import java.awt.BorderLayout;
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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import cn.com.peixunban.action.toolAction.MessBoardAction;
/**
* 留言板对话框
*
* @author 黄根华
*/
public class MessBoard extends JDialog {
private static MessBoard instance;
private JPanel southPanel;// 南部面板
private JPanel northPanel;// 北部面板
private JPanel centerPanel;// 中心面板
private JTextArea area;// 文本域
private JTextField searchText;// 查找内容
private JPanel buttonPanel;// 按钮面板
private JComboBox fontBox;// 字体列表
private ResourceBundle rb;// 包含特定语言环境的对象
private JComboBox typeBox;// 字体风格列表
private JComboBox sizeBox;// 大小列表
private Font f;// 字体;
private String fontname = "楷体";// 字体
private String type = "PLAIN";// 字体风格
private String sizeFont = "34";// 大小
/*
  * 单子设计模式
  */
private MessBoard() {
  init();
}
public static MessBoard getInstance() {
  if (instance == null) {
   instance = new MessBoard();
  }
  return instance;
}
public JTextArea getArea1() {
  return area;
}
public JTextField getSearchText() {
  return searchText;
}
/**
  * 初始化当前对话框
  */
public void init() {
  this.setTitle("留言板");
  this.setModal(true);
  this.setSize(600, 550);
  this.createFontBox();
  this.createTypeBox();
  this.createSizeBox();
  searchText = new JTextField(10);
  area = new JTextArea(50, 50);
  this.add(createNorthPanel(), BorderLayout.NORTH);
  this.add(createCenterPnael(), BorderLayout.CENTER);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
  // 添加窗口关闭事件
  this.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    dispose();
   }
  });
   this.setVisible(true);
}
/**
  * 创建中心面板
  *
  * @return
  */
public JPanel createCenterPnael() {
  centerPanel = new JPanel();
  centerPanel.setLayout(new BorderLayout());
  centerPanel.add(area, BorderLayout.CENTER);
  centerPanel.add(createButtonPanel(), BorderLayout.SOUTH);
  return centerPanel;
}
/**
  * 创建北部面板
  *
  * @return
  */
public JPanel createNorthPanel() {
  northPanel = new JPanel();
  northPanel.add(createLabel("字体  "));
  northPanel.add(fontBox);
  northPanel.add(typeBox);
  northPanel.add(sizeBox);
  northPanel.add(createButton("确定"));
  northPanel.add(createButton("颜色"));
  return northPanel;
}
/**
  * 创建按钮面板
  *
  * @return
  */
public JPanel createButtonPanel() {
  buttonPanel = new JPanel();
  buttonPanel.add(createButton("提交"));
  buttonPanel.add(createLabel("         "));
  buttonPanel.add(createButton("清空"));
  return buttonPanel;
}
/**
  * 创建按钮
  *
  * @param name
  * @return
  */
public JButton createButton(String buttonName) {
  JButton button = new JButton(buttonName);
  // 向按钮添加事件
  button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd.equals("确定")) {
     fontname = fontBox.getSelectedItem().toString();
     type = typeBox.getSelectedItem().toString();
     sizeFont = sizeBox.getSelectedItem().toString();
     area.setFont(getFont(fontname, type, sizeFont));
    } else if (cmd.equals("颜色")) {
     JColorChooser chooser = new JColorChooser();
     Color color = chooser.showDialog(MessBoard.this, "选择颜色",
       Color.BLACK);
     // 如果没有选择颜色
     if (color == null) {
      color = Color.BLACK;
     }
     area.setForeground(color);// 设置字体的颜色
    }
  });
  return button;
}
/**
  * 创建标签
  *
  * @param labelName
  * @return
  */
public JLabel createLabel(String labelName) {
  JLabel label = new JLabel(labelName, JLabel.LEFT);
  return label;
}
/**
  * 创建字体列表
  *
  * @return
  */
public void createFontBox() {
  String[] fontName = null;
  // 获得系统可用的字体
  fontName = GraphicsEnvironment.getLocalGraphicsEnvironment()
    .getAvailableFontFamilyNames();
  fontBox = new JComboBox(fontName);
  fontname = fontBox.getSelectedItem().toString();
}
/**
  * 创建字体大小的类表
  *
  * @return
  */
public void createSizeBox() {
  String fontSizes[] = { "8", "9", "10", "11", "12", "14", "16", "18",
    "20", "22", "24", "26", "28", "36", "48", "72" };
  sizeBox = new JComboBox(fontSizes);
  sizeFont = sizeBox.getSelectedItem().toString();
}
/**
  * 创建字体风格列表
  *
  * @return
  */
public void createTypeBox() {
  String[] type2 = { "PLAIN", "BOLD", "ITALIC" };
  typeBox = new JComboBox(type2);
  type = typeBox.getSelectedItem().toString();
}
/*
  * 获得字体 (non-Javadoc)
  *
  * @see java.awt.Component#getFont()
  */
public Font getFont(String name, String type, String sizefont) {
  int size = Integer.parseInt(sizeFont);
  int typle = 0;
  if (type.equalsIgnoreCase("PLAIN")) {
   typle = Font.PLAIN;
  } else if (type.equalsIgnoreCase("BOLD")) {
   typle = Font.BOLD;
  } else if (type.equalsIgnoreCase("ITALIC")) {
   typle = Font.ITALIC;
  }
  Font f = new Font(name, typle, size);
  return f;
}
/**
  * @param args
  */
  public static void main(String[] args) {
  MessBoard instance = MessBoard.getInstance();
  }
}
分享到:
评论

相关推荐

    java 实现我的记事本

     新建:新建一个文本文件  打开:调用JDK提供的“打开”... 字体:调用“字体”对话框设置文本编辑区的字体、字形和大小  颜色:调用“颜色板”对话框设置文本编辑区的颜色  帮助主题:调用网上API文档网页

    java源码包---java 源码 大量 实例

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    java api最新7.0

    java.util.prefs 此包允许应用程序存储并获取用户和系统首选项和配置数据。 java.util.regex 用于匹配字符序列与正则表达式指定模式的类。 java.util.spi java.util 包中类的服务提供者类。 java.util.zip 提供用于...

    java源码包4

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    JAVA上百实例源码以及开源项目源代码

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    java源码包3

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    JAVA上百实例源码以及开源项目

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    java源码包2

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    java开源包1

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    JAVA_API1.6文档(中文)

    java.util.prefs 此包允许应用程序存储并获取用户和系统首选项和配置数据。 java.util.regex 用于匹配字符序列与正则表达式指定模式的类。 java.util.spi java.util 包中类的服务提供者类。 java.util.zip 提供...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java波浪文字制作方法及源代码 1个目标文件 摘要:Java源码,初学实例,波浪文字 Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,...

    java 基于iText的PDF输出(源码)

    设置字体颜色 设置表单域颜色 组件方法调用: addTempl(String templKey,String templFileName),注册一个模板文件; addText(String templKey,String key,String text), 向模板中一个表单域添加一个文本数据; ...

    java开源包11

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    java开源包6

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    java开源包9

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    java开源包4

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    java开源包101

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

    java开源包5

    Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...

Global site tag (gtag.js) - Google Analytics