Jtextarea如何设置不同字体、颜色
展开全部
JTextArea不具备这项功能,可以使用JTextPane,操作Document控制JTextPane显示的内容
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Editor extends JFrame {
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane jTextPane1 = new JTextPane();
JToolBar jToolBar1 = new JToolBar();
JButton jButton1 = new JButton();
Hashtable actionTable = new Hashtable();
//String[] ActionNames = {"font-italic", "font-bold", "font-underline","font-size-24"};
private StyledDocument document = (StyledDocument)jTextPane1.getDocument();
private SimpleAttributeSet attributes = new SimpleAttributeSet();
JButton jButton2 = new JButton();
JPopupMenu jPopupMenu_FontFamily = new JPopupMenu();
JPopupMenu jPopupMenu_FontSize = new JPopupMenu();
public static void main(String[] args) {
Editor t = new Editor();
t.setLocation(50, 30);
t.setSize(500, 200);
t.show();
}
public Editor() {
try {
jbInit();
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jButton1.setText("Font Family");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("Font Size");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jTextPane1.setSelectedTextColor(new Color(255,255,255));
jTextPane1.setSelectionColor(new Color(57,102,160));
jTextPane1.setText("jTextPane1");
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextPane1, null);
this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.loadActionTable();
this.createToolBar();
}
void jButton1_actionPerformed(ActionEvent e) {
this.jPopupMenu_FontFamily.show(jButton1,jButton1.getX(),jButton1.getY()+jButton1.getHeight());
}
void jButton2_actionPerformed(ActionEvent e) {
this.jPopupMenu_FontSize.show(jButton2,jButton1.getX(),jButton2.getY()+jButton2.getHeight());
}
//==================================================================
private void loadActionTable() {
Action[] actions = this.jTextPane1.getActions();
for(int i = 0; i < actions.length; i++) {
actionTable.put(actions[i].getValue(Action.NAME), actions[i]);
/*if(actions[i].getValue(Action.NAME).toString().indexOf("justify")!= -1)
System.out.println( actions[i].getValue(Action.NAME));//i + " :" +*/
}
}
private void createToolBar(){
String[] Styles = { "font-italic", "font-bold", "font-underline"};
String[] Sizes = {"font-size-8" ,"font-size-12","font-size-14",
"font-size-16","font-size-18","font-size-24",
"font-size-32","font-size-36","font-size-48"};
String[] Familys = {"font-family-SansSerif","font-family-Serif","font-family-Monospaced"};
String[] Justifys = {"left-justify","center-justify","right-justify"};
// ToolBar
jToolBar1.add(jButton1, null);
jToolBar1.add(jButton2, null);
for(int i = 0; i < Styles.length; i++)
this.jToolBar1.add((Action)this.actionTable.get(Styles[i]));
// Menu
for(int i = 0; i < Familys.length; i++)
this.jPopupMenu_FontFamily.add((Action)this.actionTable.get(Familys[i]));
System.out.println(Sizes.length);
for(int i = 0; i < Sizes.length; i++){
Action action = (Action)this.actionTable.get(Sizes[i]);
if(action != null){
this.jPopupMenu_FontSize.add(action);
System.out.println(i);
}
}
}
//================================================================
private void setCharacterColor(Color c){
StyleConstants.setForeground(attributes, c);
int start = this.jTextPane1.getSelectionStart();
int len = this.jTextPane1.getSelectedText().length();
document.setCharacterAttributes(start, len, attributes, false);
}
}
展开全部
JTextArea本身不具备这样的功能,它是纯文本组件,你可以使用JTextPane,通过操作Document文档来控制JTextPane显示的内容,下面的代码在一个JTextPane中显示了一个图标,三行文字,每行用不同的颜色和大小显示:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;
public class Test {
JFrame frame;
JTextPane textPane;
File file;
Icon image;
public Test(){
frame = new JFrame("JTextPane");
textPane = new JTextPane();
file = new File("./classes/test/icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
}
public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str ="\n" + str ;
try {
doc.insertString(doc.getLength(), str, attrSet);
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}
public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet, true);
}//字体类型
StyleConstants.setFontSize(attrSet, fontSize);
//字体大小
insert(str, attrSet);
}
public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字",Color.red,false,20);
setDocs("第二行的文字",Color.BLACK,true,25);
setDocs("第三行的文字",Color.BLUE,false,20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
frame.setSize(200,300);
frame.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;
public class Test {
JFrame frame;
JTextPane textPane;
File file;
Icon image;
public Test(){
frame = new JFrame("JTextPane");
textPane = new JTextPane();
file = new File("./classes/test/icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
}
public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str ="\n" + str ;
try {
doc.insertString(doc.getLength(), str, attrSet);
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}
public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet, true);
}//字体类型
StyleConstants.setFontSize(attrSet, fontSize);
//字体大小
insert(str, attrSet);
}
public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字",Color.red,false,20);
setDocs("第二行的文字",Color.BLACK,true,25);
setDocs("第三行的文字",Color.BLUE,false,20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
frame.setSize(200,300);
frame.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JTextArea本身不具备这样的功能,它是纯文本组件,你可以使用JTextPane,通过操作Document文档来控制JTextPane显示的内容,下面的代码在一个JTextPane中显示了一个图标,三行文字,每行用不同的颜色和大小显示: import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.text.*; import java.io.*; public class Test { JFrame frame; JTextPane textPane; File file; Icon image; public Test(){ frame = new JFrame("JTextPane"); textPane = new JTextPane(); file = new File("./classes/test/icon.gif"); image = new ImageIcon(file.getAbsoluteFile().toString()); } public void insert(String str, AttributeSet attrSet) { Document doc = textPane.getDocument(); str ="\n" + str ; try { doc.insertString(doc.getLength(), str, attrSet); } catch (BadLocationException e) { System.out.println("BadLocationException: " + e); } } public void setDocs(String str,Color col,boolean bold,int fontSize) { SimpleAttributeSet attrSet = new SimpleAttributeSet(); StyleConstants.setForeground(attrSet, col); //颜色 if(bold==true){ StyleConstants.setBold(attrSet, true); }//字体类型 StyleConstants.setFontSize(attrSet, fontSize); //字体大小 insert(str, attrSet); } public void gui() { textPane.insertIcon(image); setDocs("第一行的文字",Color.red,false,20); setDocs("第二行的文字",Color.BLACK,true,25 查看原帖>>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java swing 中JTEXTAREA不能改变字体颜色,它是纯文本组件,可以使用JTEXTPANE,通过操作DOCUMENT文档来控制JTEXTPANE显示的内容,下面的代码在一个JTEXTPANE中显示了一个图标,三行文字,每行用不同的颜色和大小显示:
IMPORT JAVAX.SWING.*;
IMPORT JAVA.AWT.*;
IMPORT JAVA.AWT.EVENT.*;
IMPORT JAVAX.SWING.TEXT.*;
IMPORT JAVA.IO.*;
PUBLIC CLASS TEST {
JFRAME FRAME;
JTEXTPANE TEXTPANE;
FILE FILE;
ICON IMAGE;
PUBLIC TEST(){
FRAME = NEW JFRAME("JTEXTPANE");
TEXTPANE = NEW JTEXTPANE();
FILE = NEW FILE("./CLASSES/TEST/ICON.GIF");
IMAGE = NEW IMAGEICON(FILE.GETABSOLUTEFILE().TOSTRING());
}
PUBLIC VOID INSERT(STRING STR, ATTRIBUTESET ATTRSET) {
DOCUMENT DOC = TEXTPANE.GETDOCUMENT();
STR ="\N" + STR ;
TRY {
DOC.INSERTSTRING(DOC.GETLENGTH(), STR, ATTRSET);
}
CATCH (BADLOCATIONEXCEPTION E) {
SYSTEM.OUT.PRINTLN("BADLOCATIONEXCEPTION: " + E);
}
}
PUBLIC VOID SETDOCS(STRING STR,COLOR COL,BOOLEAN BOLD,INT FONTSIZE) {
SIMPLEATTRIBUTESET ATTRSET = NEW SIMPLEATTRIBUTESET();
STYLECONSTANTS.SETFOREGROUND(ATTRSET, COL);
//颜色
IF(BOLD==TRUE){
STYLECONSTANTS.SETBOLD(ATTRSET, TRUE);
}//字体类型
STYLECONSTANTS.SETFONTSIZE(ATTRSET, FONTSIZE);
//字体大小
INSERT(STR, ATTRSET);
}
PUBLIC VOID GUI() {
TEXTPANE.INSERTICON(IMAGE);
SETDOCS("第一行的文字",COLOR.RED,FALSE,20);
SETDOCS("第二行的文字",COLOR.BLACK,TRUE,25);
SETDOCS("第三行的文字",COLOR.BLUE,FALSE,20);
FRAME.GETCONTENTPANE().ADD(TEXTPANE, BORDERLAYOUT.CENTER);
FRAME.ADDWINDOWLISTENER(NEW WINDOWADAPTER() {
PUBLIC VOID WINDOWCLOSING(WINDOWEVENT E) {
SYSTEM.EXIT(0);
}});
FRAME.SETSIZE(200,300);
FRAME.SETVISIBLE(TRUE);
}
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
TEST TEST = NEW TEST();
TEST.GUI();
}
}
IMPORT JAVAX.SWING.*;
IMPORT JAVA.AWT.*;
IMPORT JAVA.AWT.EVENT.*;
IMPORT JAVAX.SWING.TEXT.*;
IMPORT JAVA.IO.*;
PUBLIC CLASS TEST {
JFRAME FRAME;
JTEXTPANE TEXTPANE;
FILE FILE;
ICON IMAGE;
PUBLIC TEST(){
FRAME = NEW JFRAME("JTEXTPANE");
TEXTPANE = NEW JTEXTPANE();
FILE = NEW FILE("./CLASSES/TEST/ICON.GIF");
IMAGE = NEW IMAGEICON(FILE.GETABSOLUTEFILE().TOSTRING());
}
PUBLIC VOID INSERT(STRING STR, ATTRIBUTESET ATTRSET) {
DOCUMENT DOC = TEXTPANE.GETDOCUMENT();
STR ="\N" + STR ;
TRY {
DOC.INSERTSTRING(DOC.GETLENGTH(), STR, ATTRSET);
}
CATCH (BADLOCATIONEXCEPTION E) {
SYSTEM.OUT.PRINTLN("BADLOCATIONEXCEPTION: " + E);
}
}
PUBLIC VOID SETDOCS(STRING STR,COLOR COL,BOOLEAN BOLD,INT FONTSIZE) {
SIMPLEATTRIBUTESET ATTRSET = NEW SIMPLEATTRIBUTESET();
STYLECONSTANTS.SETFOREGROUND(ATTRSET, COL);
//颜色
IF(BOLD==TRUE){
STYLECONSTANTS.SETBOLD(ATTRSET, TRUE);
}//字体类型
STYLECONSTANTS.SETFONTSIZE(ATTRSET, FONTSIZE);
//字体大小
INSERT(STR, ATTRSET);
}
PUBLIC VOID GUI() {
TEXTPANE.INSERTICON(IMAGE);
SETDOCS("第一行的文字",COLOR.RED,FALSE,20);
SETDOCS("第二行的文字",COLOR.BLACK,TRUE,25);
SETDOCS("第三行的文字",COLOR.BLUE,FALSE,20);
FRAME.GETCONTENTPANE().ADD(TEXTPANE, BORDERLAYOUT.CENTER);
FRAME.ADDWINDOWLISTENER(NEW WINDOWADAPTER() {
PUBLIC VOID WINDOWCLOSING(WINDOWEVENT E) {
SYSTEM.EXIT(0);
}});
FRAME.SETSIZE(200,300);
FRAME.SETVISIBLE(TRUE);
}
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
TEST TEST = NEW TEST();
TEST.GUI();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询