Java -- week 13th lab -- Task 2 -- compile a FontFamily class, which gets all the available font names on the current machine and writes

Source: Internet
Author: User

Test class:
[Java]
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: compile a FontFamily class that obtains all available font names on the current machine.
* Author: Lei hengxin
* Completion date: January 1, November 20, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input Description: Write A FontFamily class that obtains all available font names on the current machine.
* Input Description: compile a dialog box FontDialog, which is a mode dialog box and adopts the BorderLayout layout.
* Input Description: contains the name of all fonts displayed in the north of a JComboBox, including a JLabel in the middle to display the font effect, including two buttons in the south.
* Enter Description: Click YES, set the font effect in the window on which the dialog box depends, and click Cancle to cancel. Compile a window FrameHaveDialog.
* Input Description: This window has a button and a dialog box. When you click this button, the FontDialog dialog box appears.
* Enter Description: The text displayed in the display area is displayed based on the text selected from the drop-down list of the user in the dialog box. Finally, write a program execution entry for testing.
* Problem description:
* Program output:
* End the comment in the program Header
*/
Package task_two;
 
Public class Test {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
New FrameHaveDialog ();
}
 
}

 
 
Mypanel class:
[Java]
Package task_two;
Import java. awt .*;
Import java. awt. event .*;
Import javax. swing .*;
Public class Mypanel extends Panel {
 
JButton button1, button2;
Public Mypanel (){
Button1 = new JButton ("Yes ");
Button2 = new JButton ("Cancle ");
Add (button1 );
Add (button2 );
}
}

 
 
FrameHaveDialog class:
[Java]
Package task_two;
 
Import java. awt. FlowLayout;
Import java. awt. Font;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. FocusListener;
 
Import javax. swing .*;
 
// This window has a button and a dialog box. When you click this button, the FontDialog dialog box appears.
// Then, based on the text selected from the drop-down list in the displayed dialog box. Finally, write a program execution entry for testing.
Public class FrameHaveDialog extends JFrame implements ActionListener {
JTextArea text1;
JButton button;
FontDialog dialog;
Public FrameHaveDialog (){
Text1 = new JTextArea (6, 15 );
Button = new JButton ("Open dialog box ");
Add (text1 );
Add (button );
Button. addActionListener (this );
Dialog = new FontDialog (this, "modify font dialog box", true );
SetLayout (new FlowLayout ());
SetBounds (200,200,200,200 );
SetVisible (true );
}
 
Public void actionreceivmed (ActionEvent e ){
// Int index = dialog. list. getSelectedIndex ();
// Text1.setFont (new Font (String) dialog. list. getItemAt (index), Font. PLAIN, 12 ));
Dialog. setVisible (true );
If (dialog. panel. button1.hasFocus () = true)
{

Int index = dialog. list. getSelectedIndex ();
Text1.setFont (new Font (String) dialog. list. getItemAt (index), Font. PLAIN, dialog. list. getFont (). getSize ()));
}

}
}

 
 
FontFamily class:
[Java]
Package task_two;
 
Import java. awt. GraphicsEnvironment;
 
// Compile a FontFamily class that obtains all available font names on the current machine.
Public class FontFamily {
Public String [] getfont (){
GraphicsEnvironment e = GraphicsEnvironment. getLocalGraphicsEnvironment ();
String [] fontName = e. getAvailableFontFamilyNames ();
Return fontName;
}
}

 
 
 
FontDialog class:
[Java]
Package task_two;
 
Import java. awt. BorderLayout;
Import java. awt. Font;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. ItemEvent;
Import java. awt. event. ItemListener;
 
Import javax. swing .*;
 
// Compile a dialog box FontDialog, which is a mode dialog box with the BorderLayout layout.
// Contains the name of all fonts in the north of JComboBox, including a JLabel in the middle to display the font effect, including two buttons in the south.
// Click YES, set the font effect in the window on which the dialog box depends, and click Cancle to cancel.
Public class FontDialog extends JDialog implements ItemListener, ActionListener {
JComboBox list;
JLabel lable;
Mypanel;
Public FontDialog (JFrame f, String s, boolean B ){
Super (f, s, B );
Panel = new Mypanel ();
List = new JComboBox ();
Lable = new JLabel ("font effects ");
FontFamily fontFamily = new FontFamily ();
String [] s1 = fontFamily. getfont ();
For (int I = 0; I <s1.length; I ++ ){
List. addItem (s1 [I]);
}

Add (list, BorderLayout. NORTH );
Add (lable, BorderLayout. CENTER );
Add (panel, BorderLayout. SOUTH );
List. addItemListener (this );
Panel. button1.addActionListener (this );
Panel. button2.addActionListener (this );
SetBounds (380,380,380,380 );
}
 

Public void itemStateChanged (ItemEvent e ){
// TODO Auto-generated method stub
If (e. getSource () = list)
{
String name = (String) list. getSelectedItem ();
// UIManager. put ("Label. font", new Font (name, Font. BOLD, 12 ));
// System. out. print (list. getItemAt (6 ));
Lable. setFont (new Font (name, Font. PLAIN, list. getFont (). getSize ()));
}

}
Public void actionreceivmed (ActionEvent e ){
If (e. getSource () = panel. button1)
{
// Int I = list. getSelectedIndex ();
// FrameHaveDialog frameHaveDialog = new FrameHaveDialog ();
// Int index = list. getSelectedIndex ();
// Lable. setFont (new Font (String) list. getItemAt (index), Font. PLAIN, list. getFont (). getSize ()));
// System. out. print (0 );
SetVisible (false );
// System. exit (0 );
}
Else if (e. getSource () = panel. button2)
{
// System. out. print (1 );
SetVisible (false );
// System. exit (0 );
}

}
 
 
 
}

 
 
Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.