打字测速程序遇到的一个颜色显示问题求教!

jiang5495 2010-03-19 09:47:13
想做一个简单的打字测速程序,遇到一个问题,
比如说想金山打字一样上面显示文章,下面输入文章,输入正确或错误,
则上面显示的文章送颜色会有相应的变化,不知这种对文章局部字的颜色变化是如何实现的。
请大家指教可行方案!
在此谢过!
...全文
98 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
huachiggak 2010-03-19
  • 打赏
  • 举报
回复
o.o 没用过 长见识了!
metsys 2010-03-19
  • 打赏
  • 举报
回复

JLabel应该也可以,用HTML显示。
laorer 2010-03-19
  • 打赏
  • 举报
回复
JTextPane 是 swing的,不过没程序给你……
jiang5495 2010-03-19
  • 打赏
  • 举报
回复
有没有用swing的,想看一下是不是想要的效果
dracularking 2010-03-19
  • 打赏
  • 举报
回复
在某些事件关联时刻重新投显字文,用诸如setForeground之类的,附个例子,不过是swt的:


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Test {

public static void main(String[] a) {
Display display = new Display();
// Create the main window
final Shell shell = new Shell(display);

shell.setLayout(new GridLayout(2, false));

final Label fontLabel = new Label(shell, SWT.NONE);
fontLabel.setText("The selected font");

Button button = new Button(shell, SWT.PUSH);
button.setText("Font...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
// Create the color-change dialog
FontDialog dlg = new FontDialog(shell);
Font font = null;
Color color = null;

if (font != null)
dlg.setFontList(fontLabel.getFont().getFontData());
if (color != null)
dlg.setRGB(color.getRGB());

if (dlg.open() != null) {
if (font != null)
font.dispose();
if (color != null)
color.dispose();

font = new Font(shell.getDisplay(), dlg.getFontList());
fontLabel.setFont(font);

color = new Color(shell.getDisplay(), dlg.getRGB());
fontLabel.setForeground(color);

shell.pack();
}
}
});

shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
jiang5495 2010-03-19
  • 打赏
  • 举报
回复
谢了,
其实我想知道能不能用程序控制,
比方说循环什么的,或者加html标签实现之类的。
laorer 2010-03-19
  • 打赏
  • 举报
回复
JTextPane 是这个吧,

实在不行你就找下有没有开源的一些工具
laorer 2010-03-19
  • 打赏
  • 举报
回复
好像有个 richTextBox样的,里面有样式,你可以看下
jiang5495 2010-03-19
  • 打赏
  • 举报
回复
咱没人给点帮助呢??

62,615

社区成员

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

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