5

只希望字母的颜色随着一点停顿而改变(停顿可能会根据单词的时间和单词的长度而有所不同)。

下面的代码对我来说很好用。但是我认为我的逻辑混乱了。我可以理解,但我的队友应该很容易理解。

在此处输入图像描述 在此处输入图像描述

代码:

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Reminder 
{
    static   JFrame frame;
    Toolkit toolkit;
    Timer timer;
    int point=0,temp=0,hil=0,point2=0;long time1=0,time2=0;
    static   StyledDocument doc;
    static   JTextPane textpane;
    String[] arr={"Tes"," hiiii"," what"," happpn"};
    int i=0;
    int[] a=new int[5];

    public Reminder() 
    {
        a[0]=1000;
        a[1]=900;
        a[2]=300;
        a[3]=1500;
        a[4]=1700;

        ActionListener actionListener = new ActionListener() 
        {
            public void actionPerformed(ActionEvent actionEvent) 
            {
                point =arr[i].length();
                temp=point+1;
                time1=System.currentTimeMillis();
                new Thread(new t1()).start();
            }
        };

        timer = new Timer(a[i], actionListener);
        timer.setInitialDelay(0);
        timer.start();
    }

    public  class t1 implements Runnable
    {     /* true idea to use current time is beacuse i want to check and make 
sure that the time taken since the timer started, and the present time should
not  exceed the time given in the array in any case*/   
        public void run() 
        {
            try
            {
                time2=System.currentTimeMillis();
                while(time2-time1<=a[i]-200){Thread.sleep((long) (a[i] / (arr[i].length() * 4)));
                if(hil<=temp-1)
                {
                    doc.setCharacterAttributes(point2,hil, textpane.getStyle("Red"), true);}
                    hil++;
                    time2=System.currentTimeMillis();
                }
                doc.setCharacterAttributes(point2,point+1, textpane.getStyle("Red"), true);
                point2+=point;hil=0;i++;
                timer.setDelay(a[i]);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void newcompo()
    {
        JPanel panel = new JPanel();
        doc = (StyledDocument) new DefaultStyledDocument();
        textpane = new JTextPane(doc);
        textpane.setText("Test hiiii what happpn");
        javax.swing.text.Style style = textpane.addStyle("Red", null);
        StyleConstants.setForeground(style, Color.RED);
        panel.add(textpane);
        frame.add(panel);
        frame.pack();
    }

    public static void main(String args[]) throws InterruptedException
                                                                ,    InvocationTargetException 
    {
          SwingUtilities.invokeAndWait(new Runnable() 
          {
            @Override
            public void run() 
            {
                frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setVisible(true);

                newcompo();
                Reminder aa=  new Reminder();
            }
        });
    }
}

有什么建议吗?我该如何简化?

更新错误

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
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.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class KaraokeTest {

private int[] timingsArray = {1000, 900, 300, 1500};//word/letters timings
private String[] individualWordsToHighlight = {"Tes", " hiiii", " what", " happpn"};//each individual word/letters to highlight
private int count = 0;
private final JTextPane jtp = new JTextPane();
private final JButton startButton = new JButton("Start");
private final JFrame frame = new JFrame();

public KaraokeTest() {
    initComponents();
}

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

    for (String s : individualWordsToHighlight) {
        String tmp = jtp.getText();
        jtp.setText(tmp + s);
    }
    jtp.setEditable(false);

    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            startButton.setEnabled(false);
            count = 0;

            //create Arrays of individual letters and their timings
            final ArrayList<String> chars = new ArrayList<>();
            final ArrayList<Integer> charsTiming = new ArrayList<>();

            for (String s : individualWordsToHighlight) {
                for (int i = 0; i < s.length(); i++) {
                    chars.add(String.valueOf(s.charAt(i)));
                    System.out.println(String.valueOf(s.charAt(i)));
                }
            }

            for (int x = 0; x < timingsArray.length; x++) {
                for (int i = 0; i < individualWordsToHighlight[x].length(); i++) {
                    charsTiming.add(timingsArray[x] / individualWordsToHighlight[x].length());
                    System.out.println(timingsArray[x] / individualWordsToHighlight[x].length());
                }
            }

            new Timer(1, new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if (count < charsTiming.size()) {
                        highlightNextWord();
                        //restart timer with new timings
                        ((Timer) ae.getSource()).setInitialDelay(charsTiming.get(count));
                        ((Timer) ae.getSource()).restart();
                    } else {//we are at the end of the array
                        reset();
                        ((Timer) ae.getSource()).stop();//stop the timer
                    }
                    count++;//increment counter
                }
            }).start();
        }
    });

    frame.add(jtp, BorderLayout.CENTER);
    frame.add(startButton, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

private void reset() {
    startButton.setEnabled(true);
    jtp.setText("");
    for (String s : individualWordsToHighlight) {
        String tmp = jtp.getText();
        jtp.setText(tmp + s);
    }
    JOptionPane.showMessageDialog(frame, "Done");
}

private void highlightNextWord() {
    //we still have words to highlight
    int sp = 0;
    for (int i = 0; i < count + 1; i++) {//get count for number of letters in words (we add 1 because counter is only incrementd after this method is called)
        sp += 1;
    }
    //highlight words
    Style style = jtp.addStyle("RED", null);
    StyleConstants.setForeground(style, Color.RED);
    ((StyledDocument) jtp.getDocument()).setCharacterAttributes(0, sp, style, true);
}

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

在此处输入图像描述

给我例外:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - illegal start of type
    at KaraokeTest$1.actionPerformed(KaraokeTest.java:47)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6263)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6028)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4

3 回答 3

5

好的,这是您的代码的清理版本,它应该大致执行相同的操作:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Reminder {
    private static final String TEXT = "Test hiiii what happpn";
    private static final String[] WORDS = TEXT.split(" ");
    private JFrame frame;
    private Timer timer;
    private StyledDocument doc;
    private JTextPane textpane;
    private List<Integer> times = Arrays.asList(1000, 900, 300, 1500);

    private int stringIndex = 0;
    private int index = 0;

    public void startColoring() {
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                doc.setCharacterAttributes(stringIndex, 1, textpane.getStyle("Red"), true);
                stringIndex++;
                try {
                    if (stringIndex >= doc.getLength() || doc.getText(stringIndex, 1).equals(" ")) {
                        index++;
                    }
                    if (index < times.size()) {
                        double delay = times.get(index).doubleValue();
                        timer.setDelay((int) (delay / WORDS[index].length()));
                    } else {
                        timer.stop();
                        System.err.println("Timer stopped");
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        };
        timer = new Timer(times.get(index), actionListener);
        timer.setInitialDelay(0);
        timer.start();
    }

    public void initUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        doc = new DefaultStyledDocument();
        textpane = new JTextPane(doc);
        textpane.setText(TEXT);
        javax.swing.text.Style style = textpane.addStyle("Red", null);
        StyleConstants.setForeground(style, Color.RED);
        panel.add(textpane);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) throws InterruptedException, InvocationTargetException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                Reminder reminder = new Reminder();
                reminder.initUI();
                reminder.startColoring();
            }
        });
    }
}

帮助他人阅读和理解您的代码的一些技巧:

  • 使用连贯和适当的缩进(我个人尝试坚持默认的 Sun Java 约定)
  • 遵循 Java 编码约定(常量为大写,类名以大写开头,变量和方法以小写开头,使用驼峰式)
  • 使用有意义的变量和方法名称
  • 类成员应一一声明(不要使用int i, j, k;
  • 每行使用一条指令(避免if(something) doSomething(); else {doSomethingElse1(); doSomethingElse2();}单行之类的东西)
  • 避免不必要地使用static关键字(常量除外)
  • 尽量避免过多地耦合您的代码(尽量对其余代码的执行方式做出最低限度的假设)
  • 在您的代码中添加 javadoc 和注释,这始终是一个很好的做法,它对您和其他人都有很大的帮助。
于 2013-01-12T14:51:22.573 回答
4

您担心的主要原因是您没有JTextPaneEvent Dispatch Thread.

对于这种情况,当您真的想从另一个线程更新某个东西时,请始终使用EventQueue.invokeLater(...)EvenQueue.invokeAndWait(),它可以asynchronously (former)/synchronously (latter)更新您在 EDT 上的请求,但必须小心,如果没有正确使用,invokeAndWait()可能会导致。deadlocks/run conditions

这是您更新的代码,可能符合您的期望。希望您可以根据自己的喜好修改代码。

import javax.swing.*;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: Gagandeep Bali
 * Date: 1/12/13
 * Time: 5:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class ColouringText
{
    private StyledDocument document;
    private JTextPane textPane;

    private String message;
    private String[] parts;

    private Timer timer;
    private int counter;
    private int start, end;
    private Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            while (counter < parts.length)
            {
                final int len = parts[counter++].length();
                try
                {
                    EventQueue.invokeAndWait(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            document.setCharacterAttributes(
                                    start, len, textPane.getStyle("RED"), true);
                        }
                    });
                    Thread.sleep(len * 1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                }
                start += (len + 1);
            }
        }
    };

    public ColouringText()
    {
        document = (StyledDocument) new DefaultStyledDocument();
        message = "Hello there... Joey Rohan. Had you ever thought about putting indentations before pasting your code.";
        parts = message.split(" ");
        counter = 0;
        start = 0;
        end = 6;
        System.out.println("Message Length : " + message.length());
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Colouring Text Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        textPane = new JTextPane(document);
        textPane.setText(message);
        Style style = textPane.addStyle("RED", null);
        StyleConstants.setForeground(style, Color.RED);

        contentPane.add(textPane);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        thread.start();
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ColouringText().displayGUI();
            }
        });
    }
}

COLOURINGTEX 示例

于 2013-01-12T14:36:44.970 回答
3

曾经在类似任务中回答了您之前的 2 个问题:

和其他人不同意我的近距离投票,我决定对此进行一些研究。

我已经采用了我的最新示例并进行了编辑以尽可能简化等,以使您更易于理解,这反过来又应该帮助您了解如何简化自己的。

我不明白您为什么要开始一个新线程以使单词慢慢突出显示。只需使用我的其他示例并提供单个字母而不是多个字母及其时间来突出显示,因此它们将单独突出显示。或者只是制作一种方法来为您完成这项工作,并以字母和时间的形式遍历这些数组。

integer下面的示例使用s数组来保存要突出显示的单词的时间。它还包含我们要突出显示的每个单词/字母的数组:

private int[] timingsArray = {1000, 900, 300, 1500};//word/letters timings
private String[] individualWordsToHighlight = {"Tes", " hiiii", " what", " happpn"};//each individual word/letters to highlight

在我们的开始按钮中,我们有一种方法可以将上面的内容转换为每个字母的单个时间/字母,以逐个字母突出显示:

//create Arrays of individual letters and their timings
final ArrayList<String> chars = new ArrayList<>();
final ArrayList<Integer> charsTiming = new ArrayList<>();
for (String s : individualWordsToHighlight) {
    for (int i = 0; i < s.length(); i++) {
        chars.add(String.valueOf(s.charAt(i)));
        System.out.println(String.valueOf(s.charAt(i)));
    }
}

for (int x = 0; x < timingsArray.length; x++) {
    for (int i = 0; i < individualWordsToHighlight[x].length(); i++) {
        charsTiming.add(timingsArray[x] / individualWordsToHighlight[x].length());
        System.out.println(timingsArray[x] / individualWordsToHighlight[x].length());
    }
}

接下来有一个Timer将在按下开始按钮时启动的单曲,它将突出显示单词/字母,并每次都以新的延迟重新启动,直到所有单词/字母都被突出显示:

new Timer(1, new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if (count < charsTiming.size()) {
                        highlightNextWord();
                        //restart timer with new timings
                        ((Timer) ae.getSource()).setInitialDelay(charsTiming.get(count));
                        ((Timer) ae.getSource()).restart();
                    } else {//we are at the end of the array
                        reset();
                        ((Timer) ae.getSource()).stop();//stop the timer
                    }
                    count++;//increment counter
                }
            }).start();

希望这可以帮助。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
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.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class KaraokeTest {

    private int[] timingsArray = {1000, 900, 300, 1500};//word/letters timings
    private String[] individualWordsToHighlight = {"Tes", " hiiii", " what", " happpn"};//each individual word/letters to highlight
    private int count = 0;
    private final JTextPane jtp = new JTextPane();
    private final JButton startButton = new JButton("Start");
    private final JFrame frame = new JFrame();

    public KaraokeTest() {
        initComponents();
    }

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

        for (String s : individualWordsToHighlight) {
            String tmp = jtp.getText();
            jtp.setText(tmp + s);
        }
        jtp.setEditable(false);

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                startButton.setEnabled(false);
                count = 0;

                //create Arrays of individual letters and their timings
                final ArrayList<String> chars = new ArrayList<>();
                final ArrayList<Integer> charsTiming = new ArrayList<>();

                for (String s : individualWordsToHighlight) {
                    for (int i = 0; i < s.length(); i++) {
                        chars.add(String.valueOf(s.charAt(i)));
                        System.out.println(String.valueOf(s.charAt(i)));
                    }
                }

                for (int x = 0; x < timingsArray.length; x++) {
                    for (int i = 0; i < individualWordsToHighlight[x].length(); i++) {
                        charsTiming.add(timingsArray[x] / individualWordsToHighlight[x].length());
                        System.out.println(timingsArray[x] / individualWordsToHighlight[x].length());
                    }
                }

                new Timer(1, new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        if (count < charsTiming.size()) {
                            highlightNextWord();
                            //restart timer with new timings
                            ((Timer) ae.getSource()).setInitialDelay(charsTiming.get(count));
                            ((Timer) ae.getSource()).restart();
                        } else {//we are at the end of the array
                            reset();
                            ((Timer) ae.getSource()).stop();//stop the timer
                        }
                        count++;//increment counter
                    }
                }).start();
            }
        });

        frame.add(jtp, BorderLayout.CENTER);
        frame.add(startButton, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    private void reset() {
        startButton.setEnabled(true);
        jtp.setText("");
        for (String s : individualWordsToHighlight) {
            String tmp = jtp.getText();
            jtp.setText(tmp + s);
        }
        JOptionPane.showMessageDialog(frame, "Done");
    }

    private void highlightNextWord() {
        //we still have words to highlight
        int sp = 0;
        for (int i = 0; i < count + 1; i++) {//get count for number of letters in words (we add 1 because counter is only incrementd after this method is called)
            sp += 1;
        }
        //highlight words
        Style style = jtp.addStyle("RED", null);
        StyleConstants.setForeground(style, Color.RED);
        ((StyledDocument) jtp.getDocument()).setCharacterAttributes(0, sp, style, true);
    }

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

更新

回应您编辑的问题。

再次,代码对我来说很好,这让我得出结论,我们的 java 运行时不同,可能会导致问题。我的是:

java 版本“1.7.0_10” Java(TM) SE 运行时环境(构建 1.7.0_10-b18) Java HotSpot(TM) 64 位服务器 VM(构建 23.6-b04,混合模式)

更新 2:

根据您对 java 版本的评论:

JDK 6、NetBeans 6.5.1

你有没有尝试过:

final ArrayList<String> chars = new ArrayList<String>(); 
final ArrayList<Integer> charsTiming = new ArrayList<Integer>(); 

注意我<>不再使用,因为 Java 6 不支持Diamond 运算符,它必须包含数据类型,即<Integer>.

于 2013-01-12T17:59:55.577 回答