When writing your own notepad applet, you need a format dialog box to set the format for the text in it. However, in Java, there is no ready-made font format Setting dialog box for us to use. In this case, we need to design a dialog box by ourselves. Next we will introduce our implementation.
First, let's see what the effects of my dialog box are, as shown below:
The font Setting dialog box contains four parts: font selection, font selection, font size setting, and Font Style example, through the example, we can easily see the effect of the configured font.
The font selection, font selection, and font size setting are completed through three jlist controls, and a jlabel is used to display the example.
One thing to mention here is that when designing your own font Settings dialog box, do not name your class as font, because the name is included in and defined in the package that comes with the software, so it is best to change the name.
In the Design dialog box, we should first define the various controls to be used, as shown below:
// Control jlist jl_fontpolics = NULL, jl_fontstyle = NULL, jl_fontsize = NULL, jpanel jp_fontpolics = NULL, jp_fontstyle = NULL, jp_fontsize = NULL, jpanel jp_setting = NULL, jp_example = NULL, jp_examplebutton = NULL; jtextfield success = NULL, jtf_fontstyle = NULL, success = NULL; jlabel jl_examplename = NULL, jl_examplecontext = NULL; jbutton jb_ OK = NULL, jcanb_cel = NULL;
After defining the required controls, you need to create instances of various controls and add them to the dialog box. The procedure is as follows:
// Font jp_setting = new jpanel (); jp_fontpolics = new jpanel (); jp_fontstyle = new jpanel (); jp_fontsize = new jpanel (); // Add border borderpolics = borderfactory. createloweredbevelborder (); border borderstyle = borderfactory. createloweredbevelborder (); border bordersize = borderfactory. createloweredbevelborder (); borderpolics = borderfactory. createtitledborder (borderpolics, "font"); borderstyle = borderfactory. createtitledborder (borderstyle, ""); bordersize = borderfactory. createtitledborder (bordersize, "size"); jp_font+s.setborder (borderpolics); jp_fontstyle.setborder (borderstyle); define (bordersize); // define and obtain the list content Java. AWT. font [] fonts = Java. AWT. graphicsenvironment. getlocalgraphicsenvironment (). getallfonts (); int fontslength = fonts. length; string [] fontpolicsnames = new string [fontslength]; string [] fontstylenames = new string [] {"regular", "bold", "italic ", "bold + italic"}; string [] fontsizenames = new string [] {"8", "9", "10", "11", "12 ", "14", "16", "18", "20", "24", "28", "32", "36", "40 ", "48"}; For (INT I = 0; I <fontslength; I ++) {fontw.snames [I] = fonts [I]. getname () ;}// create a list of jl_fontpolics = new jlist (fontjavassnames); jl_fontstyle = new jlist (fontstylenames); jl_fontsize = new jlist (fontsizenames ); // set it to the single-choice mode jl_fontpolics.setselectionmode (listselectionmodel. single_selection); jl_fontstyle.setselectionmode (listselectionmodel. single_selection); jl_fontsize.setselectionmode (listselectionmodel. single_selection); // set to display five jl_font+s.setvisiblerowcount (5); jl_fontstyle.setvisiblerowcount (5); jl_fontsize.setvisiblerowcount (5 ); // set the default value of the text box to jtf_fontpolics = new jtextfield (font1_snames [fontslength-11]); // The default value is jtf_fontstyle = new jtextfield (fontstylenames [0]); // by default, the regular font type jtf_fontsize = new jtextfield (fontsizenames [9]) is selected; // the size of the 24 th sign is selected by default. // Add the jl_fontuncs.addlistselectionlistener (ipvslistener) for the list ); extends (sizelistener); // enables the list item to move up and down jscrollpane blocks = new jscrollpane (blocks); extends jsp_fontstyle = new jscrollpane (jl_fontstyle); extends jsp_fontsize = new limit ); jp_fontpolics.setlayout (New boxlayout (jp_fontpolics, boxlayout. page_axis); jp_fontstyle.setlayout (New boxlayout (jp_fontstyle, boxlayout. page_axis); jp_fontsize.setlayout (New boxlayout (jp_fontsize, boxlayout. page_axis); // Add the control to the panel layout (layout); Aggregate (jsp_fontpolics); Aggregate (jtf_fontstyle); Aggregate (jsp_fontstyle); Aggregate (jtf_fontsize); Aggregate (jsp_fontsize ); topology (jp_fontpolics); jp_setting.add (jp_fontstyle); jp_setting.add (jp_fontsize); jp_example = new jpanel (New borderlayout (); jp_examplebutton = new jpanel (); jl_examplename = new jlabel ("Example:"); jl_examplecontext = new jlabel ("aabbccddee", jbutton. center); textfont = new font ("", Font. plain, 24); annotate (textfont); jb_ OK = new jbutton ("OK"); jb_cancel = new jbutton ("quit"); jp_examplebutton.add (jb_ OK); jp_examplebutton.add (jb_cancel ); jp_example.add (jl_examplename, borderlayout. north); jp_example.add (jl_examplecontext, borderlayout. center); jp_example.add (jp_examplebutton, borderlayout. south); this. add (jp_setting, borderlayout. north); this. add (jp_example, borderlayout. center );
At this time, we have completed the basic interface design. Next we need to complete the design of three jlist and two jbutton event listeners, and add listeners. The program is as follows:
// The font listener listselectionlistener extends slistener = new listselectionlistener () {public void valuechanged (listselectionevent e) {// Add such a program, prevent the valuechanged function from being called twice when you click if (E. getvalueisadjusting () {return;} jlist polics = (jlist) E. getsource (); string signature sname = (string) polics. getselectedvalue (); jtf_fontpolics.settext (policsname); settextfont (); Aggregate (textfont) ;}}; // The listselectionlistener stylelistener listener () {public void valuechanged (listselectionevent e) {// note that this program is added to prevent the valuechanged function from being called twice if (E. getvalueisadjusting () {return;} jlist style = (jlist) E. getsource (); string stylename = (string) style. getselectedvalue (); define (stylename); settextfont (); jl_examplecontext.setfont (textfont) ;}}; // listselectionlistener sizelistener = new listselectionlistener () {public void valuechanged (listselectionevent e) {// note that this program is added to prevent the valuechanged function from being called twice if (E. getvalueisadjusting () {return;} jlist size = (jlist) E. getsource (); string sizename = (string) size. getselectedvalue (); jtf_fontsize.settext (sizename); settextfont (); jl_examplecontext.setfont (textfont );}};
The event listening handler for the two buttons is relatively simple, as shown below:
// Event listener public void actionreceivmed (actionevent e) {// todo auto-generated method stubif (E. getactioncommand (). equals ("yes") {This. nodepad. jta_message.setfont (textfont); this. dispose ();} else if (E. getactioncommand (). equals ("no") {This. dispose ();}}
As described above, I think everyone knows how to write their own font setting applications!