JTextArea 中如何设置不同的文本格式 我是在application中开发,如果JTextArea无法实现,可以用什么类来实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //你得用JTextPane,下面是一个例子。import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.text.*;import java.io.*;public class Test { JFrame frame, frame2; JTextPane textPane,textPane2; File file; Icon image; public Test(){ frame = new JFrame("JTextPane"); frame2 = new JFrame("提取的内容"); textPane = new JTextPane(); textPane2 = new JTextPane(); file = new File("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); }}); frame2.getContentPane().add(textPane2, BorderLayout.CENTER); frame.setSize(200,300); frame.setVisible(true); frame2.setLocation(200, 0); frame2.setSize(200,300); frame2.setVisible(true); } public static void main(String[] args) { Test test = new Test(); test.gui(); StyledDocument docs = test.textPane.getStyledDocument();//取得textPane中的StyledDocument类文档 test.textPane2.setStyledDocument(docs);//将StyledDocument类文档传给textPane2 }} 可以通过设置前景色实现JTextArea tArea;tArea.setForeground(Color.blue); 引用那点问题 SWT菜单的显示问题 如何实现从一加到100? PipedOutput(Input)Stream传递非byte类型数据 困扰我一个下午的Check算法,希望能够得到各位的帮助。 谁能提供几个国外比较厉害的java论坛? 关于线程的综合使用问题(希望你能帮助我哦^^) 关于JBuilder7的问题 新手提问:怎样开发Java的b/s模式的系统? 我想用jbuilder开发一个邮件系统(服务器),有兴趣的朋友请进来。。。 如何将一个Integer转成int 型? 比如现在时间str="2004-04-29 12:30:00" 那么12000秒后时间是多少,最后变成字符串形式
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Test {
JFrame frame, frame2;
JTextPane textPane,textPane2;
File file;
Icon image; public Test(){
frame = new JFrame("JTextPane");
frame2 = new JFrame("提取的内容");
textPane = new JTextPane();
textPane2 = new JTextPane();
file = new File("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);
}});
frame2.getContentPane().add(textPane2, BorderLayout.CENTER);
frame.setSize(200,300);
frame.setVisible(true);
frame2.setLocation(200, 0);
frame2.setSize(200,300);
frame2.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
StyledDocument docs = test.textPane.getStyledDocument();//取得textPane中的StyledDocument类文档
test.textPane2.setStyledDocument(docs);//将StyledDocument类文档传给textPane2
}
}
JTextArea tArea;
tArea.setForeground(Color.blue);