首页 / JAVA / 突出显示Java中的单词
突出显示Java中的单词
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了突出显示Java中的单词,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6794字,纯文字阅读大概需要10分钟。
内容图文
我正在尝试突出显示一个单词,但是第一次只显示.length()-2,延迟然后显示最后两个单词.突出显示第一个单词,但延迟后不突出显示最后两个单词.请帮助.
这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class newkarao {
private int[] timings = {600, 1000, 400,50};
private String[] words = new String[]{"Hellojjkhl", "java", "whoooooh","bye"};
private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
private int count = 0;
private int xx=0,tlength,spe;
private boolean fisrTime = true;
private JFrame frame;
private JTextPane jtp;
JButton startButton;
public newkarao() {
initComponents();
}
private void initComponents() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
jtp = new JTextPane();
for (String s : words) {
String tmp = jtp.getText();
if (tmp.equals("")) {
jtp.setText(s);
} else {
jtp.setText(tmp + " " + s);
}
}
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
startKaraoke();
}
});
frame.add(jtp, BorderLayout.CENTER);
frame.add(startButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
private void startKaraoke() {
if (fisrTime) {
startButton.setEnabled(false);
fisrTime = false;
}
new Thread(new Runnable() {
@Override
public void run() {
Timer t = null;
try {
t = createAndStartTimer(timings[count], count);
} catch (InterruptedException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
while (t.isRunning()) {//wait for timer to be done
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
count++;
if (count == timings.length) {
JOptionPane.showMessageDialog(frame, "Done");
startButton.setEnabled(true);
count = 0;
} else {
startKaraoke();
}
}
});
}
}).start();
}
private Timer createAndStartTimer(int delay, final int count) throws InterruptedException {
try {
int sp = 0;
for (int i = 0; i < count; i++) {
sp += words[i].length() + 1;
spe=sp;
}
// int a=timings[xx];xx++;
System.out.println("jfd");
tlength=words[count].length();//Thread.currentThread().sleep(5000);
jtp.getHighlighter().addHighlight(sp, sp + words[count].length()-2, highlightPainter);
System.out.println(sp + words[count].length()-2);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
Timer t = new Timer(delay, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
try {System.out.println(spe + words[count].length());
jtp.getHighlighter().addHighlight(spe, spe + words[count].length(), highlightPainter);
System.out.println("sleep");
// Thread.currentThread().sleep(5000);
jtp.getHighlighter().removeAllHighlights();
} catch (BadLocationException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t.setRepeats(false);
t.start();
return t;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new newkarao();
}
});
}
}
解决方法:
我不认为你了解我.
使用my newest example,我做了这个(请注意,这是上一个问题中示例的更新代码.现在仅使用Swing计时器):
private int[] timings = {2000, 4000, 0, 3000, 2000};//word timings
private String[] words = {"Hel", "lo", " ", "wor", "ld"};//each indiviaul word
private String sentence = "Hello world";//entire string for writing to JSCrollPane
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class KaraokeTest {
private int[] timings = {2000, 4000, 0, 3000, 2000};//word timings
private String[] words = {"Hel", "lo", " ", "wor", "ld"};//each indiviaul word
private String sentence = "Hello world";//entire string for writing to JSCrollPane
private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
private int count = 0;
private boolean fisrTime = true;
private JFrame frame;
private JTextPane jtp;
private JButton startButton;
private AtomicBoolean done = new AtomicBoolean(false);
public KaraokeTest() {
initComponents();
}
private void initComponents() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
jtp = new JTextPane();
jtp.setText(sentence);
jtp.setEditable(false);
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
startKaraoke();
}
});
frame.add(jtp, BorderLayout.CENTER);
frame.add(startButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
private void startKaraoke() {
if (fisrTime) {
startButton.setEnabled(false);
fisrTime = false;
}
createAndStartTimer(timings[count], count);
Timer t = new Timer(1, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
if (done.get()) {
count++;
if (count == timings.length) {
JOptionPane.showMessageDialog(frame, "Done");
startButton.setEnabled(true);
count = 0;
fisrTime = true;
done.getAndSet(false);
((Timer) ae.getSource()).stop();
} else {
((Timer) ae.getSource()).stop();
startKaraoke();
}
}
}
});
done.getAndSet(false);//to synchronize when the remove highlight timer is done so a clash between adding highlights before the timer is done doesnt occur
t.start();
}
private void createAndStartTimer(int delay, final int count) {
int sp = 0;
for (int i = 0; i < count; i++) {
sp += words[i].length();
}
try {
jtp.getHighlighter().addHighlight(sp, sp + words[count].length(), highlightPainter);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
Timer t = new Timer(delay, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
jtp.getHighlighter().removeAllHighlights();
done.getAndSet(true);//so that out other timer knows we are done completly and can add new higlights
}
});
t.setRepeats(false);
t.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new KaraokeTest();
}
});
}
}
内容总结
以上是互联网集市为您收集整理的突出显示Java中的单词全部内容,希望文章能够帮你解决突出显示Java中的单词所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】