highlighting the word in java

后端 未结 2 1019
日久生厌
日久生厌 2021-01-16 13:11

I am triying to highlight a word, but only .length()-2 at 1st time,delay and then last 2 words.The first words are highlighted but it is not highlighting the last two words

2条回答
  •  误落风尘
    2021-01-16 13:50

    You can achieve this with one Timer as well by simply adjusting the delay for the Timer. Just took the code from @DavidKroukamp and removed one of the Timer instances

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DefaultHighlighter;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.concurrent.atomic.AtomicBoolean;
    
    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 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;
        }
    
        Timer t = new Timer( timings[0], new ActionListener() {
          private int currentIndex = 0;
          @Override
          public void actionPerformed( ActionEvent e ) {
            clearHighlights();
            highlightWord( currentIndex );
            currentIndex++;
            if ( currentIndex < timings.length ){
              ( ( Timer ) e.getSource() ).setDelay( timings[currentIndex] );
              ( ( Timer ) e.getSource() ).restart();
            } else {
              ( ( Timer ) e.getSource() ).stop();
            }
          }
        } );
        t.setRepeats( true );
    
        t.start();
      }
    
      private void highlightWord( int index ){
        int sp = 0;
        for (int i = 0; i < index; i++) {
          sp += words[i].length();
        }
        try {
          jtp.getHighlighter().addHighlight(sp, sp + words[index].length(), highlightPainter);
        } catch (BadLocationException ex) {
          ex.printStackTrace();
        }
      }
      private void clearHighlights(){
        jtp.getHighlighter().removeAllHighlights();
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            new KaraokeTest();
          }
        });
      }
    }
    

提交回复
热议问题