java - Setting the font of a JTextArea -
i want create text editor in java. here i've written far:
package com.thundercrust.applications; import java.awt.borderlayout; public class texteditor implements actionlistener{ private static string[] fontoptions = {"serif", "agency fb", "arial", "calibri", "cambrian", "century gothic", "comic sans ms", "courier new", "forte", "garamond", "monospaced", "segoe ui", "times new roman", "trebuchet ms", "serif"}; private static string[] sizeoptions = {"8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28"}; imageicon newicon = new imageicon("res/newicon.png"); imageicon saveicon = new imageicon("res/saveicon.png"); imageicon openicon = new imageicon("res/openicon.png"); imageicon fonticon = new imageicon("res/fonticon.png"); imageicon changefonticon = new imageicon("res/changefonticon.png"); jbutton new = new jbutton(newicon); jbutton save = new jbutton(saveicon); jbutton open = new jbutton(openicon); jbutton changefont = new jbutton(changefonticon); jlabel fontlabel = new jlabel(fonticon); jlabel fontlabeltext = new jlabel("font: "); jlabel fontsizelabel = new jlabel("size: "); jcombobox <string> fontname = new jcombobox<>(fontoptions); jcombobox <string> fontsize = new jcombobox<>(sizeoptions); jtoolbar tool = new jtoolbar(); jtextarea texty = new jtextarea(); jscrollpane scroll = new jscrollpane(texty); private static final int width = 1366; private static final int height = width / 16 * 9; private static string name = "text editor"; private static jframe frame = new jframe(); public void display() { frame.settitle(name); frame.setsize(width, height); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setresizable(false); frame.setvisible(true); new.addactionlistener(this); new.settooltiptext("creates new file"); save.addactionlistener(this); save.settooltiptext("saves current file"); open.addactionlistener(this); open.settooltiptext("opens file"); changefont.addactionlistener(this); changefont.settooltiptext("change font"); fontlabel.settooltiptext("font"); fontlabeltext.settooltiptext("set kind of font"); fontsizelabel.settooltiptext("set size of font"); tool.add(new); tool.addseparator(); tool.add(save); tool.addseparator(); tool.add(open); tool.addseparator(); tool.addseparator(); tool.addseparator(); tool.add(fontlabel); tool.addseparator(); tool.add(fontlabeltext); tool.add(fontname); tool.addseparator(); tool.add(fontsizelabel); tool.add(fontsize); tool.addseparator(); tool.add(changefont); jpanel pane = new jpanel(); pane.setlayout(new borderlayout()); pane.add(tool, "north"); pane.add(scroll, "center"); frame.setcontentpane(pane); } public static void main(string args[]) { texteditor editor = new texteditor(); editor.display(); } public void actionperformed(actionevent evt) { string fontnameset; string fontsizesettemp; int fontsizeset; object source = evt.getsource(); if(source == new) { texty.settext(""); } else if(source == changefont) { fontnameset = (string) fontname.getselecteditem(); fontsizesettemp = (string) fontsize.getselecteditem(); fontsizeset = integer.parseint(fontsizesettemp); system.out.println(fontnameset + fontsizeset); scroll.setfont(new font(fontnameset, fontsizeset, font.plain)); } } }
my problem setting fonts jtextarea
. when click changefont
button on actual program, nothing happens. do?
new font(fontnameset, fontsizeset, font.plain)
this has arguments in wrong order. should be:
new font(fontnameset, font.plain, fontsizeset)
also, mentioned in comment:
scroll.setfont(new font(fontnameset, fontsizeset, font.plain));
should be:
texty.setfont(new font(fontnameset, font.plain, fontsizeset));
Comments
Post a Comment