java实现仿windows 字体设置选项卡实例
程序员文章站
2024-03-12 09:34:26
想用java做一个像windows里一样的txt编辑软件,涉及到字体设置选项卡,在网上找了很久都没找到,就生气啦自己写一个,现在贴这里分享一下,下次再遇到这样的问题就不用自...
想用java做一个像windows里一样的txt编辑软件,涉及到字体设置选项卡,在网上找了很久都没找到,就生气啦自己写一个,现在贴这里分享一下,下次再遇到这样的问题就不用自己亲自打代码啦!
package 实验; 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; /** * 字体格式设置对话框 */ 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("李涛"), new string("abc") }; private static jframe frame; public font font, newfont;// 字体类 private color color;// 颜色类 color newcolor; private jfilechooser filechoose = new jfilechooser();// 文件选择类实例 private jdialog colordlg;// 颜色对话框 private jcolorchooser colorchoose = new jcolorchooser();// 颜色选择类实例 private graphicsenvironment environment; // 该类中又获取系统字体的方法; private string[] fontnameset;// 字体‘逻辑名'集 // 字体‘样式'集的字符串数组 private string[] fontstyleset = { "常规", "倾斜", "加粗", "倾斜 加粗" }; // 字体‘样式'集的常量数组 private integer[] fontcon = { font.plain, font.italic, font.bold, font.bold | font.italic }; // 字体‘大小'集 private string[] fontsizeset = { "6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" }; public static void main(string args[]) {// 主函数 fontformat a = new fontformat(); a.setvisible(true); } public fontformat() {// 无参构造函数 super(frame, "李涛—字体设置窗口", true); frame = new jframe(); initgui(); } public fontformat(jframe frame) {// 含参构造函数 super(frame, "李涛—字体设置窗口", true); this.frame = frame;// 父窗口中必须有一个public的font对象 // setalwaysontop(true); initgui(); } private void initgui() {// 字体格式选择器的界面初始化 try { getcontentpane().setlayout(null); environment = graphicsenvironment.getlocalgraphicsenvironment();// graphicsenvironment是一个抽象类,不能实例化,只能用其中的静态方法获取一个实例 fontnameset = environment.getavailablefontfamilynames();// 获取系统字体 addmenu();// 加入菜单 initfont();// 初始化字体 // pack(); setsize(380, 337); setdefaultcloseoperation(windowconstants.dispose_on_close); setwindowpos();// 使窗口屏幕居中 setresizable(false);// 大小不可变 } catch (exception e) { e.printstacktrace(); } } private void initfont() {// 初始化字体 // 设置默认字体格式为父窗口font对向的字体格式 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("12", true); font = new font(fontnameset[0], fontcon[0], 12); newfont = font;// 保存原来的字体格式 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());// 改text styletx.settext(fontstyleset[idxstyle]); sizetx.settext("" + frame.getfont().getsize()); namelt.setselectedvalue(frame.getfont().getname(), true);// 改list显示 stylelt.setselectedindex(idxstyle); sizelt.setselectedvalue("" + frame.getfont().getsize(), true); font = new font(fontnameset[0], fontcon[0], 12);// 保存当前格式 newfont = font;// 保存原来的字体格式 prestx.setfont(font);// 预览中设为当前模式 } } private void addmenu() {// 加入菜单 // 4个lable--------------------------------------------------------------------------------- namelb = new jlabel(); getcontentpane().add(namelb); namelb.settext("字体:"); namelb.setbounds(10, 14, 120, 26); namelb.setfont(new java.awt.font("simsun", 1, 14)); stylelb = new jlabel(); getcontentpane().add(stylelb); stylelb.settext("字型:"); stylelb.setbounds(151, 14, 120, 23); stylelb.setfont(new java.awt.font("simsun", 1, 14)); sizelb = new jlabel(); getcontentpane().add(sizelb); sizelb.settext("大小:"); sizelb.setbounds(275, 14, 79, 24); sizelb.setfont(new java.awt.font("simsun", 1, 14)); preslb = new jlabel(); getcontentpane().add(preslb); preslb.settext("预览:"); 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个下拉条--+监听----------------------------------------------------------------------------- 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) { styleltmouseclicked(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) { sizeltmouseclicked(evt); } }); }// ------------------------------------------------------------------------------------- // 中英选项(--------------------------------------------------------------------------------- languageg = new buttongroup(); language[0] = new jradiobutton("中"); getcontentpane().add(language[0]); language[0].setselected(false);// 初始化显示 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.settext(slanguage[0]); } }); language[1] = new jradiobutton("英"); 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.settext(slanguage[1]); } }); // 3个按钮+监听--------------------------------------------------------------------------------- // 确定按钮 approve = new jbutton(); getcontentpane().add(approve); approve.settext("确定"); 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) { approveactionperformed(evt); } }); // 取消按钮 cancel = new jbutton(); getcontentpane().add(cancel); cancel.settext("取消"); 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) { cancelactionperformed(evt); } }); // 颜色选择按钮 chose = new jbutton(); getcontentpane().add(chose); chose.settext("颜色"); 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) { choseactionperformed(evt); } });// ------------------------------------------------------------------------- } private void setwindowpos() {// 窗口居中 toolkit kit = toolkit.getdefaulttoolkit();// 抽象类,通过静态方法获取实例 dimension framesize = new dimension(), screensize = kit.getscreensize(); // 获取屏幕的大小 getsize(framesize); // 获取窗口大小 setlocation((screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2); } private void nameltmouseclicked(mouseevent evt) {// 字体逻辑名列表的鼠标单击事件 nametx.settext(namelt.getselectedvalue().tostring()); font = new font(nametx.gettext(), font.getstyle(), font.getsize()); prestx.setfont(font); } private void styleltmouseclicked(mouseevent evt) {// 字体样式列表的鼠标单击事件 string temp = stylelt.getselectedvalue().tostring(); 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) {// 字体大小列表的鼠标点击事件 sizetx.settext(sizelt.getselectedvalue().tostring()); font = new font(font.getname(), font.getstyle(), integer.parseint(sizetx.gettext())); prestx.setfont(font); } private void approveactionperformed(actionevent evt) {// 确定按钮的触发事件 string name = nametx.gettext(); int style = fontcon[stylelt.getselectedindex()]; int size = integer.parseint(sizetx.gettext()); font = new font(name, style, size); frame.setfont(font); // 父窗口的font对象 newfont = font;// 更新原来保存格式 newcolor = color;// 更新颜色 this.dispose(); } private void cancelactionperformed(actionevent evt) {// 取消按钮的触发事件 this.dispose(); } private void choseactionperformed(actionevent evt) {// 颜色选择触发事件 if (colordlg == null) { colordlg = jcolorchooser.createdialog(fontformat.this, "select text color", true, colorchoose, new coloroklistener(), null); } colorchoose.setcolor(color = prestx.getforeground()); colordlg.setvisible(true); } class coloroklistener implements actionlistener {// 重写颜色按钮点击监听类覆盖接口actionlistener public void actionperformed(actionevent e) { color c = colorchoose.getcolor(); color = c; prestx.setforeground(c); prestx.repaint(); } } }
效果如下:
希望本文所述对你有所帮助,java仿windows 字体设置选项卡内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习java可以继续关注本站。