java 设置字体格式

java 设置字体格式,第1张

Java Swing中可以给每个控件设置字体格式和其他属性的设置,示例如下:

submit= new JButton("登陆")

submit.setFont(new Font("宋体", Font.PLAIN, 16))

三个参数分别表示: 字体,样式(粗体,斜体等),字号

submit.setForeground(Color.RED)

这个表示给组件上的文字设置颜色Color.RED表示红色

当然你也可以自己给RGB的值 比如 submit.setForeground(new Color(215,215,200))

java中没有自带的字体对话框,这需要自己来编写。

text.setFond("字体名字",字形(如,fond.bold),大小)

import java.awt.*

import java.awt.event.*

import javax.swing.JColorChooser

//import javax.swing.border.*

class ff extends Frame implements ActionListener

{

Choice font,size,bolder

Button bb

ff(String s)

{

setTitle(s)

font=new Choice()

bolder=new Choice()

size=new Choice()

//bolder.add加监视器

//font.add加监视器

//size.add加监视器

Panel p1=new Panel()

Panel p2=new Panel()

bb=new Button("点击打开")

bb.addActionListener(this)

p1.setLayout(new GridLayout(4,1))

p2.setLayout(new GridLayout(4,1))

GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment()

String ss[]=gg.getAvailableFontFamilyNames()

String bold[]={"Font.BOLD","Font.CENTER_BASELINE","Font.CENTER_BASELINE","Font.ITALIC",

"Font.PLAIN","Font.ROMAN_BASELINE","Font.TRUETYPE_FONT"}

for(int i=126i<ss.lengthi++)

font.add(ss[i])

for(int i=12i<=64i+=2)

{

String w=String.valueOf(i)

size.add(w)

}

for(int i=0i<bold.lengthi++)

{

bolder.add(bold[i])

}

p1.add(new Label("请选择字体"))

p1.add(font)

p1.add(new Label("请选择大小"))

p1.add(size)

p2.add(new Label("请选择字型"))

p2.add(bolder)

p2.add(new Label("请选择字体颜色"))

p2.add(bb)

add(p2,BorderLayout.WEST)

add(p1,BorderLayout.EAST)

setSize(250,150)

setVisible(true)

pack()

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent ee)

{

System.exit(0)

}

})

}

public void actionPerformed(ActionEvent e)

{

Color cc=JColorChooser.showDialog(this,"颜色对话框",null)

bb.setBackground(cc)//应用举例

}

}

public class font

{

public static void main(String[] args)

{

new ff("字体对话框")

}

}

这是我自己改的,你可以参考,我用的是simsun,你只需要修改最后一行,把路径指向simsun.ttf所在的路径就可以了,注意需要有fonts.scale和fonts.dir。

ysj@yjGentoo resources $ cat /opt/sun-jre-1.4.2.04/lib/font.properties

# @(#)font.properties.zh_CN_UTF8.Sun.linux 1.1 03/07/14

# Copyright 2003 Sun Microsystems, Inc. All rights reserved.

#

# Component Font Mappings For Linux zh_CN_UTF8

#

serif.0=-b&h-lucidabright-medium-r-normal--*-%d-*-*-p-*-iso8859-1

serif.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

serif.italic.0=-b&h-lucidabright-medium-i-normal--*-%d-*-*-p-*-iso8859-1

serif.italic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

serif.bold.0=-b&h-lucidabright-demibold-r-normal--*-%d-*-*-p-*-iso8859-1

serif.bold.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

serif.bolditalic.0=-b&h-lucidabright-demibold-i-normal--*-%d-*-*-p-*-iso8859-1

serif.bolditalic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.0=-b&h-lucidasans-medium-r-normal-sans-*-%d-*-*-p-*-iso8859-1

sansserif.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.italic.0=-b&h-lucidasans-medium-i-normal-sans-*-%d-*-*-p-*-iso8859-1

sansserif.italic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.bold.0=-b&h-lucidasans-bold-r-normal-sans-*-%d-*-*-p-*-iso8859-1

sansserif.bold.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.bolditalic.0=-b&h-lucidasans-bold-i-normal-sans-*-%d-*-*-p-*-iso8859-1

sansserif.bolditalic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

monospaced.0=-b&h-lucidatypewriter-medium-r-normal-sans-*-%d-*-*-m-*-iso8859-1

monospaced.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

monospaced.italic.0=-b&h-lucidatypewriter-medium-r-normal-sans-*-%d-*-*-m-*-iso8859-1

monospaced.italic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

monospaced.bold.0=-b&h-lucidatypewriter-bold-r-normal-sans-*-%d-*-*-m-*-iso8859-1

monospaced.bold.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

monospaced.bolditalic.0=-b&h-lucidatypewriter-bold-r-normal-sans-*-%d-*-*-m-*-iso8859-1

monospaced.bolditalic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialog.0=-b&h-lucidasans-medium-r-normal-sans-*-%d-*-*-p-*-iso8859-1

dialog.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialog.italic.0=-b&h-lucidasans-medium-i-normal-sans-*-%d-*-*-p-*-iso8859-1

dialog.italic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialog.bold.0=-b&h-lucidasans-bold-r-normal-sans-*-%d-*-*-c-*-iso8859-1

dialog.bold.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialog.bolditalic.0=-b&h-lucidasans-bold-i-normal-sans-*-%d-*-*-c-*-iso8859-1

dialog.bolditalic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialoginput.0=-b&h-lucidatypewriter-medium-r-normal-sans-*-%d-*-*-m-*-iso8859-1

dialoginput.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialoginput.italic.0=-b&h-lucidatypewriter-medium-r-normal-sans-*-%d-*-*-m-*-iso8859-1

dialoginput.italic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialoginput.bold.0=-b&h-lucidatypewriter-bold-r-normal-sans-*-%d-*-*-m-*-iso8859-1

dialoginput.bold.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

dialoginput.bolditalic.0=-b&h-lucidatypewriter-bold-r-normal-sans-*-%d-*-*-m-*-iso8859-1

dialoginput.bolditalic.1=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

# Missing Glyph Character

#

default.char=274f

# Component Font Character Encodings

#

fontcharset.serif.0=sun.io.CharToByteISO8859_1

fontcharset.serif.1=sun.io.CharToByteUnicodeBigUnmarked

fontcharset.sansserif.0=sun.io.CharToByteISO8859_1

fontcharset.sansserif.1=sun.io.CharToByteUnicodeBigUnmarked

fontcharset.monospaced.0=sun.io.CharToByteISO8859_1

fontcharset.monospaced.1=sun.io.CharToByteUnicodeBigUnmarked

fontcharset.dialog.0=sun.io.CharToByteISO8859_1

fontcharset.dialog.1=sun.io.CharToByteUnicodeBigUnmarked

fontcharset.dialoginput.0=sun.io.CharToByteISO8859_1

fontcharset.dialoginput.1=sun.io.CharToByteUnicodeBigUnmarked

# Exclusion Ranges

#

# XFontSet Information

#

fontset.serif.plain=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.serif.italic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.serif.bold=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.serif.bolditalic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.plain=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.italic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.bold=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.bolditalic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.monospaced.plain=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.monospaced.italic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.monospaced.bold=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.monospaced.bolditalic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialog.italic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialog.bold=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialog.bolditalic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialog.plain=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialoginput.italic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialoginput.bold=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialoginput.bolditalic=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.dialoginput.plain=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.default=\

-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1

filename.-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1=/usr/share/fonts/ttf/China/simsun.ttf

appendedfontpath=/usr/share/fonts/ttf/China


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11394017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存