突出显示java中的单词

时间:2012-11-13 12:02:39

标签: java swing text highlight

我希望突出显示特定时间的字词(就像在卡拉OK应用中一样)。 每个单词将有一个特定的时间突出显示。我能够采取时间,但没有在哪里。如何突出一个单词持续时间。搜索stackoverflow和谷歌,但死胡同。 我可以借助java脚本或HTML吗? 请帮帮我。 这是一段代码我如何计时:

millis mil=new millis();
if(true){

if(flag==1)
    flag=0;
else flag=1;
if(flag==0)
value=mil.done(flag,start);
start=value;
if(flag==1)
value=mil.done(flag,start);//function to calculate duration
}System.out.println("val:"+(value-3086610));
   // System.out.println("gyjghjghjghj"+(System.nanoTime()-start2));
    String s = textArea.getText();

    char[] words=s.toCharArray();


    for(i=last;words[i]!=' '&&words[i]!='\n';i++,last=i)
    {

    }
 try {//System.out.println(acount);
            hilit.addHighlight(first, last, painter);

        last++;   first=last;

        } catch (BadLocationException ex) {
            Logger.getLogger(newh.class.getName()).log(Level.SEVERE, null, ex);
        }

这就是我能够为每个单词计时的方法 谢谢。

2 个答案:

答案 0 :(得分:4)

您可以从链接http://java-sl.com/blink.html

开始

向JTextArea添加高光并让它们闪烁一段时间。

答案 1 :(得分:4)

+1给StanislavL回答。

一个简短的例子希望它有所帮助。

在这里,我创造了我的话语和时间:

int[] timings = {2000, 1000, 4000};
String[] words = new String[]{"Hello", "java", "whoooooh"};

点击开始按钮后:

enter image description here

2000毫秒后:

enter image description here

1000后:

enter image description here

4000毫秒后:

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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, 1000, 4000};
    private String[] words = new String[]{"Hello", "java", "whoooooh"};
    private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
    private int count = 0;
    private boolean fisrTime = true;
    private JFrame frame;
    private JTextPane jtp;
    JButton startButton;

    public KaraokeTest() {
        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 = createAndStartTimer(timings[count], count);

                while (t.isRunning()) {//wait for timer to be done
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }

                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) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                int sp = 0;
                for (int i = 0; i < count; i++) {
                    sp += words[i].length() + 1;
                }
                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();
            }
        });
        t.setRepeats(false);
        t.start();
        return t;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new KaraokeTest();
            }
        });
    }
}

<强>更新

修正了上述代码,以便能够在指定的时间内突出显示句子中的单个字符:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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, 1000, 4000, 2000, 3000};//char timings
    private String[] words = {"H", "e", "l", "l", "o"};//each indiviaul word
    private String sentence = "Hello";//entire string for writing to JSCrollPane
    private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
    private int count = 0;
    private boolean fisrTime = true;
    private JFrame frame;
    private JTextPane jtp;
    JButton startButton;

    public KaraokeTest() {
        initComponents();
    }

    private void initComponents() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        jtp = new JTextPane();

        jtp.setText(sentence);

        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 = createAndStartTimer(timings[count], count);

                while (t.isRunning()) {//wait for timer to be done
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        count++;
                        if (count == timings.length) {
                            JOptionPane.showMessageDialog(frame, "Done");
                            startButton.setEnabled(true);
                            count = 0;
                            fisrTime = true;
                        } else {
                            startKaraoke();
                        }
                    }
                });

            }
        }).start();
    }

    private Timer createAndStartTimer(int delay, final int count) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                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();
            }
        });
        t.setRepeats(false);
        t.start();
        return t;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new KaraokeTest();
            }
        });
    }
}