Java implementation Imitation Windows font Settings tab Instance _java

Source: Internet
Author: User
Tags gettext int size

Want to do a Java like windows in the same TXT editing software, related to the Font Settings tab, found on the internet for a long time did not find, angry at their own write a, now posted here to share, the next time you encounter such problems do not have to personally hit the code!

Package experiment;
Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import javax.swing.*;

Import Javax.swing.border.BevelBorder;
  /** * Font Formatting dialog box * * * public class Fontformat extends JDialog {private JLabel namelb;
  Private JLabel Stylelb;
  Private JLabel Sizelb;
  Private JLabel preslb;
  Private JTextField Nametx;
  Private JTextField Styletx;
  Private JTextField Sizetx;
  Private JTextField Prestx;
  Private JList Namelt;
  Private JList Stylelt;
  Private JList Sizelt;
  Private JScrollPane jScrollPane1;
  Private JScrollPane JScrollPane2;
  Private JScrollPane JScrollPane3;
  Private JButton approve;
  Private JButton Cancel;
  Private JButton chose;
  Private jradiobutton[] Language = new jradiobutton[2];
  Private Buttongroup Languageg;

  private string slanguage[] = {new String ("Li Tao"), new String ("ABC")};
  private static JFrame frame; Public font font, Newfont//font class private color color;//color newcolor; Private JFileChooser filechoose = new JFileChooser ()//File Select class instance private JDialog colordlg;//Color dialog box Private Jcolorchoose R colorchoose = new JColorChooser ();//Color Select class instance private graphicsenvironment environment; A method for obtaining system fonts in this class; private string[] fontnameset;//font ' logical name ' Set//font ' style ' set of string array private string[] Fontstyleset = {general,
  "Tilt", "bold", "Tilt Bold"}; Constant array of font ' style ' set private integer[] Fontcon = {font.plain, font.italic, Font.Bold, Font.Bold |
  Font.Italic}; Font ' size ' Set private string[] Fontsizeset = {"6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "2

  4 "," 26 "," 28 "," 36 "," 48 "," 72 "};
    public static void Main (String args[]) {//main function Fontformat a = new Fontformat ();
  A.setvisible (TRUE);
    Public Fontformat () {//parameterless constructor Super (frame, "Li Tao-font Settings window", true);
    frame = new JFrame ();
  Initgui (); Public Fontformat (JFrame frame) {//Include parameter constructor super (frame, Li Tao-font settings window), true);
    This.frame = frame;//must have a public font object//Setalwaysontop (TRUE) in the parent window;
  Initgui ();
      The interface of private void Initgui () {//font format Selector Initializes the try {Getcontentpane (). setlayout (NULL); Environment = Graphicsenvironment.getlocalgraphicsenvironment ();//Graphicsenvironment is an abstract class that cannot be instantiated, Only one of the static methods can be used to get an instance Fontnameset = Environment.getavailablefontfamilynames ();//Get System font AddMenu ();//Add menu INI
      TFont ();//Initialize Font/pack ();
      SetSize (380, 337);
      Setdefaultcloseoperation (Windowconstants.dispose_on_close);
    SetWindowPos ();//Make the window screen centered setresizable (false);//Size invariant} catch (Exception e) {e.printstacktrace (); }} private void Initfont () {//Initialize font//Set default font format for font alignment of parent window if (frame.getfont () = null) {Nametx.
      SetText (Fontnameset[0]);
      Styletx.settext (Fontstyleset[0]);
      Sizetx.settext ("12");
      Namelt.setselectedvalue (Fontnameset[0], true);
      Stylelt.setselectedindex (0); Sizelt.setselEctedvalue ("A", true);
      Font = new Font (fontnameset[0], fontcon[0], 12);
      Newfont = font;//Save the original font format prestx.setfont (font);
    Joptionpane.showmessagedialog (NULL, "CCAC");
      else {int idxstyle = 0; for (int i = 0; i < fontcon.length i++) {if (fontcon[i] = = Frame.getfont (). GetStyle ()) Idxstyle = i
      ;
      } nametx.settext (Frame.getfont (). GetName ());//Change Text Styletx.settext (Fontstyleset[idxstyle));
      Sizetx.settext ("" + Frame.getfont (). GetSize ());
      Namelt.setselectedvalue (Frame.getfont (). GetName (), true);//Change list display Stylelt.setselectedindex (Idxstyle);
      Sizelt.setselectedvalue ("" + Frame.getfont (). GetSize (), true); Font = new Font (fontnameset[0], fontcon[0], 12;//Save the current format Newfont = font;//Save the original font format prestx.setfont (font); The preview is set to the current mode}} private void AddMenu () {//Add menu//4 lable----------------------------------------------------- ----------------------------NameLb = new JLabel ();
    Getcontentpane (). Add (Namelb);
    Namelb.settext ("font:");
    Namelb.setbounds (10, 14, 120, 26);

    Namelb.setfont (New Java.awt.Font ("SimSun", 1, 14));
    Stylelb = new JLabel ();
    Getcontentpane (). Add (Stylelb);
    Stylelb.settext ("font:");
    Stylelb.setbounds (151, 14, 120, 23);

    Stylelb.setfont (New Java.awt.Font ("SimSun", 1, 14));
    Sizelb = new JLabel ();
    Getcontentpane (). Add (Sizelb);
    Sizelb.settext ("Size:");
    Sizelb.setbounds (275, 14, 79, 24);

    Sizelb.setfont (New Java.awt.Font ("SimSun", 1, 14));
    PRESLB = new JLabel ();
    Getcontentpane (). Add (PRESLB);
    Preslb.settext ("preview:");
    Preslb.setbounds (151, 150, 120, 80);

    Preslb.setfont (New Java.awt.Font ("SimSun", 1, 14)); 4 TextField---------------------------------------------------------------------------------nametx = new
    JTextField ();
    Nametx.seteditable (FALSE);
    Getcontentpane (). Add (NAMETX);

    Nametx.setbounds (10, 42, 120, 22); Styletx = new JTextField();
    Styletx.seteditable (FALSE);
    Getcontentpane (). Add (STYLETX);

    Styletx.setbounds (151, 42, 100, 21);
    Sizetx = new JTextField ();
    Sizetx.seteditable (FALSE);
    Getcontentpane (). Add (SIZETX);

    Sizetx.setbounds (275, 42, 79, 22);
    PRESTX = new JTextField ();
    Prestx.seteditable (FALSE);
    Getcontentpane (). Add (PRESTX);
    Prestx.setbounds (151, 200, 203, 61);

    Prestx.settext (slanguage[1]); 3 Pull-down--+ listening-----------------------------------------------------------------------------jScrollPane1 = new
    JScrollPane ();
    Getcontentpane (). Add (JSCROLLPANE1);
    Jscrollpane1.setbounds (10, 74, 120, 210);
      {Listmodel Fontnamemodel = new DefaultComboBoxModel (fontnameset);
      Namelt = new JList ();
      Jscrollpane1.setviewportview (Namelt);
      Namelt.setmodel (Fontnamemodel);
      Namelt.setbounds (274, 193, 90, 86);
      Namelt.setborder (borderfactory createetchedborder (bevelborder.lowered)); Namelt.addmouselistener (New MouseadApter () {public void mouseclicked (MouseEvent evt) {nameltmouseclicked (evt);
    }
      });
    } jScrollPane2 = new JScrollPane ();
    Getcontentpane (). Add (JScrollPane2);
    Jscrollpane2.setbounds (151, 74, 100, 103);
      {Listmodel Fontstylemodel = new DefaultComboBoxModel (fontstyleset);
      Stylelt = new JList ();
      Jscrollpane2.setviewportview (Stylelt);
      Stylelt.setmodel (Fontstylemodel);
      Stylelt.setbounds (310, 215, 70, 102);
      Stylelt.setborder (borderfactory createetchedborder (bevelborder.lowered)); Stylelt.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent evt) {STYLELTMOUSECLI
        Cked (EVT);
    }
      });
    } JScrollPane3 = new JScrollPane ();
    Getcontentpane (). Add (JScrollPane3);
    Jscrollpane3.setbounds (275, 75, 79, 100);
      {Listmodel Fontsizemodel = new DefaultComboBoxModel (fontsizeset);
      Sizelt = new JList (); Jscrollpane3.setviewporTview (Sizelt);
      Sizelt.setmodel (Fontsizemodel);
      Sizelt.setbounds (300, 218, 54, 102);
      Sizelt.setborder (borderfactory createetchedborder (bevelborder.lowered)); Sizelt.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent evt) {Sizeltmouseclick
        Ed (evt);
    }
      }); }//-------------------------------------------------------------------------------------//Chinese-English option (----------------
    -----------------------------------------------------------------Languageg = new Buttongroup ();
    Language[0] = new Jradiobutton ("Medium");
    Getcontentpane (). Add (Language[0]);
    Language[0].setselected (false);//Initialize display language[0].setbounds (271, 179, 40, 20);
    Language[0].setfont (New Java.awt.Font ("SimSun", 1, 12));
    Languageg.add (Language[0]); Language[0].addactionlistener (new ActionListener () {public void actionperformed (ActionEvent evt) {prestx.s
      Ettext (Slanguage[0]);

    }
    }); LaNGUAGE[1] = new Jradiobutton ("English");
    Getcontentpane (). Add (Language[1]);
    Language[1].setselected (TRUE);
    Language[1].setbounds (321, 179, 40, 20);
    Language[1].setfont (New Java.awt.Font ("SimSun", 1, 12));
    Languageg.add (language[1]); Language[1].addactionlistener (new ActionListener () {public void actionperformed (ActionEvent evt) {prestx.s
      Ettext (slanguage[1]);

    }
    }); 3 button + Listener---------------------------------------------------------------------------------//OK button approve = new
    JButton ();
    Getcontentpane (). Add (approve);
    Approve.settext ("OK");
    Approve.setbounds (151, 265, 67, 20);
    Approve.setfont (New Java.awt.Font ("kaiti_gb2312", 1, 12)); Approve.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent evt) {Approveactio
      Nperformed (EVT);

    }
    });
    Cancel button Cancel = new JButton ();
    Getcontentpane (). Add (cancel);
    Cancel.settext ("cancellation"); Cancel.setbounds (219, 265, 67, 20);
    Cancel.setfont (New Java.awt.Font ("kaiti_gb2312", 1, 12)); Cancel.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent evt) {Cancelactionp
      Erformed (EVT);

    }
    });
    Color selection button chose = new JButton ();
    Getcontentpane (). Add (chose);
    Chose.settext ("color");
    Chose.setbounds (287, 265, 67, 20);
    Chose.setfont (New Java.awt.Font ("kaiti_gb2312", 1, 12)); Chose.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent evt) {Choseactionper
      Formed (EVT); })//-------------------------------------------------------------------------} private void SetWindowPos () { Window Center Toolkit kit = Toolkit.getdefaulttoolkit ()//abstract class, get instance through static method Dimension framesize = new Dimension (), ScreenS ize = Kit.getscreensize (); Gets the size of the screen getsize (framesize); Get Window Size setlocation ((screensize.width-framesize.width)/2, (Screensize.height-framesize.height)/2); } private void nameltmouseclicked (MouseEvent evt) {//font logical Name list mouse click event Nametx.settext (Namelt.getselectedvalue (). tostr
    ing ());
    Font = new Font (Nametx.gettext (), Font.getstyle (), font.getsize ());
  Prestx.setfont (font); The private void styleltmouseclicked (MouseEvent evt) {//Font style list mouse click event String temp = Stylelt.getselectedvalue (). tostr
    ING ();
    Styletx.settext (temp);
    int index = 0;
    while (Index < 4 &&!fontstyleset[index].equals (temp)) {index++;
    Font = new Font (Font.getname (), Fontcon[index], font.getsize ());
  Prestx.setfont (font); } private void sizeltmouseclicked (MouseEvent evt) {//font size list of mouse clicks event Sizetx.settext (Sizelt.getselectedvalue (). Tostri
    Ng ());
    Font = new Font (Font.getname (), Font.getstyle (), Integer.parseint (Sizetx.gettext ());
  Prestx.setfont (font);
    } private void Approveactionperformed (ActionEvent evt) {//OK button trigger Event String name = Nametx.gettext (); int style = Fontcon[stylelt.geTselectedindex ()];
    int size = Integer.parseint (Sizetx.gettext ());
    Font = new Font (name, style, size); Frame.setfont (font);
  The parent window's Font object Newfont = font;//updates the original save format Newcolor = color;//update color this.dispose ();
  } private void Cancelactionperformed (ActionEvent evt) {//Cancel button trigger event This.dispose (); } private void Choseactionperformed (ActionEvent evt) {//color selection trigger event if (Colordlg = = null) {Colordlg = Jcolorch Ooser.createdialog (Fontformat.this, "Select Text Color", True, Colorchoose, New Coloroklistener (), NUL
    L);
    } colorchoose.setcolor (color = Prestx.getforeground ());
  Colordlg.setvisible (TRUE); Class Coloroklistener implements ActionListener {//Rewrite color button click Listener Class overwrite interface ActionListener public void actionperformed (ACT
      Ionevent e) {Color c = colorchoose.getcolor ();
      color = c;
      Prestx.setforeground (c);
    Prestx.repaint ();

 }
  }
}

The effect is as follows:

I hope this article will help you, Java imitation Windows font Settings tab content is introduced here. I hope you will continue to pay attention to our website! Want to learn Java can continue to focus on this site.

Related Article

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.