jtextArea里怎么设置文本颜色

dd121011 2008-09-19 11:16:09
rt。。
...全文
4303 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
yph007595 2012-04-05
  • 打赏
  • 举报
回复
遇到同样的问题,想问楼主最后怎么解决这问题的啊?谢谢。
dawn023 2008-09-27
  • 打赏
  • 举报
回复
你还是用JEditorPane或者JTextPane吧,JTextArea不支持你要的需求。
rewafei 2008-09-26
  • 打赏
  • 举报
回复
jeditpane
jeditpane.setCharacterAttributes(MutableAttributeSet"样式变量", false);
使用StyleConstants设置MutableAttributeSet变量
cowputer 2008-09-22
  • 打赏
  • 举报
回复
建议你看看sun的demo,
http://java.sun.com/docs/books/tutorialJWS/uiswing/components/ex6/TextDemo.jnlp,这个是执行demo的地址
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TextFieldDemoProject/src/components/TextFieldDemo.java
这个是源代码
horizonlyhw 2008-09-19
  • 打赏
  • 举报
回复 1
JTextArea_RULE.setBackground(Color.PINK); 設置背景
JTextArea_RULE.setForeground(Color.BLUE); 設置字體
酒浪子 2008-09-19
  • 打赏
  • 举报
回复
学习!
dd121011 2008-09-19
  • 打赏
  • 举报
回复
我试试变不变色
<html> <font cotlor= red >test </font> </html>

我在jTextArea2里输入:我试试变不变色
然后ctrl + enter后
jTextArea1里显示的文字。。
dd121011 2008-09-19
  • 打赏
  • 举报
回复
/*
* FrameTest.java
*
* Created on 2008年9月10日, 上午10:14
*/

package com.dong.test;

import java.awt.Color;

/**
*
* @author Administrator
*/
public class FrameTest extends javax.swing.JFrame {

/** Creates new form FrameTest */
public FrameTest() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("\u53d1\u9001");
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
kSend(evt);
}
});
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
send(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setName("");
jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
KeySend(evt);
}
});
jTextArea2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
KSendTextArea(evt);
}
});

jScrollPane2.setViewportView(jTextArea2);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1))
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(39, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 209, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(25, 25, 25))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(jButton1)
.add(33, 33, 33))))
);
pack();
}// </editor-fold>

private void KeySend(java.awt.event.KeyEvent evt) {
// TODO 将在此处添加您的处理代码:
if(evt.isControlDown()&&evt.getKeyCode()==10){
String text = jTextArea2.getText();
jTextArea1.append(text);
jTextArea1.append("\n");
//jTextArea1.setForeground(Color.blue);
//jTextArea1.append("<html><font color =red>test</font></html>");
//jTextArea1.append("<html><font color =red>test</font></html> ");
// jTextArea1.append(" <html><font cotlor= red >test </font></html> ");
jTextArea1.append(" <html> <font cotlor= red >test </font> </html> ");
jTextArea1.append("\n");
jTextArea2.setText("");
}


}

private void KSendTextArea(java.awt.event.MouseEvent evt) {
// TODO 将在此处添加您的处理代码:

}

private void kSend(java.awt.event.KeyEvent evt) {
// TODO 将在此处添加您的处理代码:
if(evt.isControlDown()&&evt.getKeyCode()==10){
String text = jTextArea2.getText();
System.out.println("Test testarea2;" + jTextArea2.getText());
jTextArea1.append(text);
jTextArea1.setForeground(Color.blue);
System.out.println("Test testarea1;" + jTextArea1.getText());

}
}

private void send(java.awt.event.MouseEvent evt) {
// TODO 将在此处添加您的处理代码:
String text = jTextArea2.getText();
System.out.println("Test testarea2;" + jTextArea2.getText());

jTextArea1.setText(text);
System.out.println("Test testarea1;" + jTextArea1.getText());
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameTest().setVisible(true);
}
});
}

// 变量声明 - 不进行修改
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// 变量声明结束

}

哈哈。。所有的代码发给你看看。。
dd121011 2008-09-19
  • 打赏
  • 举报
回复
 String text = jTextArea2.getText();
jTextArea1.append(text);
jTextArea1.append("\n");
//jTextArea1.setForeground(Color.blue);
//jTextArea1.append("<html><font color =red>test</font></html>");
//jTextArea1.append("<html><font color =red>test</font></html> ");
// jTextArea1.append(" <html><font cotlor= red >test </font></html> ");
jTextArea1.append(" <html> <font cotlor= red >test </font> </html> ");
jTextArea1.append("\n");
jTextArea2.setText("");

确实不行啊。。。你看我写的是不是有问题啊?
他把html的源代码给打印出来了。。
<html> <font cotlor= red >test </font> </html>
horizonlyhw 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dd121011 的回复:]
我是用Swing 写的。。。。jTextArea1是swing里的组件。。。。
[/Quote]
。。。。。。當然知道是swing的了 swing是可以識別html語言的
jTextArea1.append(" <html> <font cotlor= red >test </font> </html> ");
dd121011 2008-09-19
  • 打赏
  • 举报
回复
swing里的TextArea好象不支持html标签把。。
dd121011 2008-09-19
  • 打赏
  • 举报
回复
我是用Swing 写的。。。。jTextArea1是swing里的组件。。。。
xlbc 2008-09-19
  • 打赏
  • 举报
回复
就是这样

jTextArea1.append(" <html><font cotlor= red >test </font></html> ");
dd121011 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 horizonlyhw 的回复:]
Java code
String text = jTextArea2.getText();
jTextArea1.append(text);//这行兰色
jTextArea1.append("\n");
jTextArea1.setForeground(Color.blue);
jTextArea1.append("<html><font color =red>test</font></html> jTextArea1.append("\n");
jTextArea2.setText("");
[/Quote]
这个在jTestArea1里显示的是:<html><font color =red>test</font></html>
jTextArea解释不了html标签啊
xlbc 2008-09-19
  • 打赏
  • 举报
回复
回8楼,html的东西,用<html></html>括死就行了。
dd121011 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 horizonlyhw 的回复:]
沒看到你那個藍色的 你自己照著改下就好了
[/Quote]
jTextArea1.append("<font cotlor= red >test</font> ");
这个样子出来不变色啊 。。把整个字符串给打印出来了。。。
<font cotlor= red >test</font>
horizonlyhw 2008-09-19
  • 打赏
  • 举报
回复
沒看到你那個藍色的 你自己照著改下就好了
horizonlyhw 2008-09-19
  • 打赏
  • 举报
回复

String text = jTextArea2.getText();
jTextArea1.append(text);//这行兰色
jTextArea1.append("\n");
jTextArea1.setForeground(Color.blue);
jTextArea1.append("<html><font color =red>test</font></html> jTextArea1.append("\n");
jTextArea2.setText("");
dd121011 2008-09-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 horizonlyhw 的回复:]
用html 的 <font color= >標簽把你想要換顏色的字括起來就可以了
[/Quote]
html标签里的东东在jframe里也可以用啊?
dd121011 2008-09-19
  • 打赏
  • 举报
回复
比如:
String text = jTextArea2.getText();
jTextArea1.append(text);//这行兰色
jTextArea1.append("\n");
jTextArea1.setForeground(Color.blue);
jTextArea1.append("test ");//我想单独设置这行的颜色。。红色
jTextArea1.append("\n");
jTextArea2.setText("");
加载更多回复(2)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧