java修改JFrame默认字体方式
程序员文章站
2022-06-18 13:40:04
目录java修改jframe默认字体jframe基本参数设置java修改jframe默认字体修改默认字体的方法很简单。首先我们随便写一个按钮出来:import javax.swing.*; publi...
java修改jframe默认字体
修改默认字体的方法很简单。首先我们随便写一个按钮出来:
import javax.swing.*; public class test{ static final int width = 300; static final int height = 200; public static void main(string[] args){ jframe jf = new jframe(); jf.setvisible(true); jf.setsize(width,height); jf.setdefaultcloseoperation(jframe.exit_on_close); jpanel jp = new jpanel(); jf.setcontentpane(jp); jbutton jb = new jbutton("确定"); jp.add(jb); jf.pack(); } }
接着我们可以加上这么一行:
uimanager.put("button.font", new java.awt.font("宋体", 0, 12));
开头的import加上:
import javax.swing.uimanager;
这样就可以改字体了。
字体类型和大小可以随便改,只要电脑里有安装就行了。“0”位置代表的是字体风格,一般不做修改。(如斜体、加粗等,要修改的话请自行查阅api……我也不太记得了。。)
不建议使用网上的个性字体,因为这样做出来的软件程序到别的电脑上用会产生字体方面的问题。而且尽量少用生僻字,以免某些字体库里没有收录对应的字形。
自定义的字体可以进行一下封装:(下面的部分摘自网络)
public class fontclass { public static void loadindyfont() { uimanager.put("checkbox.font", new java.awt.font("宋体", 0, 12)); uimanager.put("tree.font", new java.awt.font("宋体", 0, 12)); uimanager.put("viewport.font", new java.awt.font("宋体", 0, 12)); uimanager.put("progressbar.font", new java.awt.font("宋体", 0, 12)); uimanager.put("radiobuttonmenuitem.font", new java.awt.font("宋体", 0, 12)); uimanager.put("formattedtextfield.font", new java.awt.font("宋体", 0, 12)); uimanager.put("toolbar.font", new java.awt.font("宋体", 0, 12)); uimanager.put("colorchooser.font", new java.awt.font("宋体", 0, 12)); uimanager.put("togglebutton.font", new java.awt.font("宋体", 0, 12)); uimanager.put("panel.font", new java.awt.font("宋体", 0, 12)); uimanager.put("textarea.font", new java.awt.font("宋体", 0, 12)); uimanager.put("menu.font", new java.awt.font("宋体", 0, 12)); uimanager.put("radiobuttonmenuitem.acceleratorfont", new java.awt.font("宋体", 0, 12)); uimanager.put("spinner.font", new java.awt.font("宋体", 0, 12)); uimanager.put("menu.acceleratorfont", new java.awt.font("宋体", 0, 12)); uimanager.put("checkboxmenuitem.acceleratorfont", new java.awt.font("宋体", 0, 12)); uimanager.put("tableheader.font", new java.awt.font("宋体", 0, 12)); uimanager.put("textfield.font", new java.awt.font("宋体", 0, 12)); uimanager.put("optionpane.font", new java.awt.font("宋体", 0, 12)); uimanager.put("menubar.font", new java.awt.font("宋体", 0, 12)); uimanager.put("button.font", new java.awt.font("宋体", 0, 12)); uimanager.put("label.font", new java.awt.font("宋体", 0, 12)); uimanager.put("passwordfield.font", new java.awt.font("宋体", 0, 12)); uimanager.put("internalframe.titlefont", new java.awt.font("宋体", 0, 12)); uimanager.put("optionpane.buttonfont", new java.awt.font("宋体", 0, 12)); uimanager.put("scrollpane.font", new java.awt.font("宋体", 0, 12)); uimanager.put("menuitem.font", new java.awt.font("宋体", 0, 12)); uimanager.put("tooltip.font", new java.awt.font("宋体", 0, 12)); uimanager.put("list.font", new java.awt.font("宋体", 0, 12)); uimanager.put("optionpane.messagefont", new java.awt.font("宋体", 0, 12)); uimanager.put("editorpane.font", new java.awt.font("宋体", 0, 12)); uimanager.put("table.font", new java.awt.font("宋体", 0, 12)); uimanager.put("tabbedpane.font", new java.awt.font("宋体", 0, 12)); uimanager.put("radiobutton.font", new java.awt.font("宋体", 0, 12)); uimanager.put("checkboxmenuitem.font", new java.awt.font("宋体", 0, 12)); uimanager.put("textpane.font", new java.awt.font("宋体", 0, 12)); uimanager.put("popupmenu.font", new java.awt.font("宋体", 0, 12)); uimanager.put("titledborder.font", new java.awt.font("宋体", 0, 12)); uimanager.put("combobox.font", new java.awt.font("宋体", 0, 12)); }
然后我们只需要在我们想要使用的时候,加上一句:
fontclass.loadindyfont();
就行了~
jframe基本参数设置
import java.io.ioexception; import java.awt.font; import java.awt.event.windowevent; import java.awt.event.windowlistener; import javax.swing.jframe; import javax.swing.imageicon; import javax.swing.uimanager; import javax.imageio.imageio; public class frame implements windowlistener { public static jframe frame; public frame() { initialize(); } private void initialize(){ // 新建窗体 frame = new jframe(); // 设置窗体自动调节大小 frame.pack(); // 设置窗体位置、大小 frame.setbounds(100, 100, 100, 100); // 设置窗体是否可调节大小 frame.setresizable(false); // 设置窗体布局 frame.getcontentpane().setlayout(null); // 设置窗体标题 frame.settitle("frame"); // 设置窗体字体 frame.setfont(new font("黑体", font.plain, 17)); // 设置窗体在屏幕*打开 frame.setlocationrelativeto(null); // 设置窗体默认关闭方式为退出程序 frame.setdefaultcloseoperation(jframe.exit_on_close); // 设置窗体图标 frame.seticonimage(imageio.read(this.getclass().getresource("/priv/image/image.png"))); // 设置窗体观感(皮肤/主题) uimanager.setlookandfeel("com.sun.java.swing.plaf.windows.windowslookandfeel"); // 设置窗体是否可见 frame.setvisible(true); // 添加 windowlistener frame.addwindowlistener(this); } /** * @description 重写 windowlistener * @param e */ public void windowclosing(windowevent e) { } public void windowclosed(windowevent e) { } public void windowopened(windowevent e) { } public void windowiconified(windowevent e) { } public void windowdeiconified(windowevent e) { } public void windowactivated(windowevent e) { } public void windowdeactivated(windowevent e) { } public static void main(string args[]) { new frame(); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。