Java调用系统的字体和颜色

xiaoxiao2022-06-11  31

  下面的这个程序是项目里面的一个对话框,可以直接改成窗体,将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();  }}

转载请注明原文地址: https://www.6miu.com/read-4932112.html

最新回复(0)