(1) Function Description:
Glyph class font is used to standardize the font size, style, and font used by the component. Its constructors:
Public Font (String name,int style,int size);
Name indicates a locally available font name
Style represents the font style, containing three font.plain,font.bold,font.italic, corresponding to the flat, bold, and italic respectively.
A useful way to get locally available fonts
Graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment ();
String[] Fa=ge.getavailablefontfamilynames ();
Array groups are obtained from the drawing environment to locally available fonts.
(2) The code displays the word on the form with the specified font constraint.
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
public class Graexp extends JFrame {cinstead c1=new cinstead ();
Container C;
String[] ZT;
String[] zx={"flat body", "bold", "Italic"};
String name= "Serif";
int type=0;
int size=12;
JLabel lbl1=new JLabel ("font:");
JLabel lbl2=new JLabel ("Glyph:");
JLabel lbl3=new JLabel ("Font size:");
JLabel lbl=new JLabel ("Test word abcABC123");
JComboBox CB1, Cb2=new JComboBox (ZX);
JTextField tf1=new JTextField (+), Tf2=new JTextField (10); Public Graexp () {//Get an array of available font names Graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment (); zt=
Ge.getavailablefontfamilynames ();
Cb1=new JComboBox (ZT);
Setcontentpane (C1);
C=getcontentpane ();
C.setlayout (New FlowLayout (Flowlayout.left));
C.add (LBL1);
C.add (CB1);
C.add (LBL2);
C.add (CB2);
C.add (LBL3);
C.add (TF1);
Lbl.setpreferredsize (New Dimension (200,60));
Lbl.setforeground (color.red);
C.add (LBL); Cb1.additemlistener (New ItemListener () {public void itemstatechanged (ItemEvent event) {int state=event.getstatechange ();
Name= (String) Event.getitem ();
Setcustomfont (name,type,size);
}
} ); Cb2.additemlistener (New ItemListener () {public void itemstatechanged (ItemEvent event) {int State=event.getstatechange
();
String s= (String) Event.getitem ();
if (S.equals ("flat")) {Type=font.plain} else if (S.equals ("bold")) {Type=font.bold;} else {type=font.italic;}
Setcustomfont (name,type,size);
}
} ); Tf1.addkeylistener (New Keyadapter () {public void keypressed (KeyEvent e) {int c=e.getkeycode (); if (c==10) {String s=
Tf1.gettext ();
Size=integer.parseint (s);
Setcustomfont (name,type,size);
}
}
} );
Setdefaultcloseoperation (Windowconstants.dispose_on_close);
SetSize (New Dimension (400,300));
Show ();
public static void Main (string[] args) {graexp ge=new graexp ();}
private void Setcustomfont (String name,int type,int size) {Lbl.setfont (new Font (name,type,size)); Lbl.revalidate ();}
Class Cinstead extends JPanel {ImageIcon icon;
Image img; Public Cinstead () {icon=New ImageIcon (CInstead.class.getResource ("333.png"));
Img=icon.getimage ();
public void Paintcomponent (Graphics g) {super.paintcomponent (g); G.drawimage (img,0,0,null);} }
}
Program Effect: