Java线程与JTextArea无法正常工作

时间:2014-11-05 09:57:37

标签: java string multithreading jtextarea

在Java中,我正在创建一个登录/注册程序(希望将来扩展它)但是遇到了问题。

我试图以动画类型的作家风格时尚在屏幕上显示文字。

当我运行类的主要方法时,文本看起来很好,当我在keylistener中运行它时,它会失败并等待文本出现,然后更新框架以便我可以看到它。

下面的源代码:

Login.class

public class Login implements KeyListener {

int BACK_WIDTH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int BACK_HEIGHT = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

JFrame back_frame = new JFrame();

LoginTerminal login = new LoginTerminal();

public Login() {
    back_frame.setSize(BACK_WIDTH, BACK_HEIGHT);
    back_frame.setLocation(0, 0);
    back_frame.getContentPane().setBackground(Color.BLACK);
    back_frame.setUndecorated(true);
    //back_frame.setVisible(true);

    back_frame.addKeyListener(this);
    login.addKeyListener(this);
    login.setLocationRelativeTo(null);
    login.setVisible(true);
    login.field.addKeyListener(this);

    login.slowPrint("Please login to continue...\n"
               + "Type 'help' for more information.\n");
}

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ESCAPE) {
        System.exit(0);
    }

    if(i == KeyEvent.VK_ENTER) {
        login.slowPrint("\nCommands\n"
                 + "-----------\n"
                 + "register\n"
                 + "login\n");
    }
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

LoginTerminal.class

public class LoginTerminal implements KeyListener {

CustomFrame frame = new CustomFrame(Types.TERMINAL);

JTextArea log = new JTextArea();
public JTextField field = new JTextField();

public void setVisible(boolean bool) {
    frame.setVisible(bool);
}

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}

public void slowPrint(String str) {
    for(int i = 0; i < str.length(); i++) {
        log.append("" + str.charAt(i));

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (i == str.length() - 1) {
            log.append("\n");
        }
    }
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

public LoginTerminal() {
    try {

        InputStream is = LoginTerminal.class.getResourceAsStream("/fonts/dungeon.TTF");
        Font font = Font.createFont(Font.TRUETYPE_FONT, is);
        font = font.deriveFont(Font.PLAIN, 10);

        frame.add(field);
        frame.add(log);

        log.setBackground(Color.BLACK);
        log.setForeground(Color.WHITE);
        log.setWrapStyleWord(true);
        log.setLineWrap(true);
        log.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        log.setFont(font);
        log.setEditable(false);
        log.setCaretColor(Color.BLACK);

        field.setBackground(Color.BLACK);
        field.setForeground(Color.WHITE);
        field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
        field.setFont(font);
        field.setCaretColor(Color.BLACK);
        field.addKeyListener(this);
        field.setText("  >  ");

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void dumpToLog() {
    log.append(field.getText() + "\n");
    field.setText("  >  ");
}

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ENTER && field.isFocusOwner()) {
        if(field.getText().equals("  >  HELP") || field.getText().equals("  >  help")) {
            dumpToLog();


        } else {
            dumpToLog();
        }
    }


    if(!field.getText().startsWith("  >  ")) {
        field.setText("  >  ");
    }
}

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

Main.class

public class Main {

    public static void main(String[] args) {
        new Login();
    }

}

我的问题在这里:

public Login() {
    login.slowPrint("Please login to continue...\n"
               + "Type 'help' for more information.\n");
}

当我运行它时,它按预期工作。

下面是同一个类(Login.class)

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ENTER) {
        login.slowPrint("\nCommands\n"
                 + "-----------\n"
                 + "register\n"
                 + "login\n");
    }
}

它冻结并等待完成。

我认为它可能是Thread.sleep(50);在LoginTerminal.class中作为标题状态,因为这是触发输入动画的事实。

希望我在这里说清楚。 谢谢大家的帮助!

修改

这是导致定时器错误的原因......

public void timerPrint(String text) {
Timer timer = new Timer(50, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (index < text.length() - 1 && index >= 0) {
            String newChar = Character.toString(text.charAt(index));
            textArea.append(newChar);
            index++;
        } else {
            textArea.setText(null);
            index = 0;
           // You could stop the timer here...
        }
   }
});
timer.start();
}

构造函数Timer(int,new ActionListener(){})未定义

编辑编辑,全班:

package com.finn.frametypes;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;

import com.finn.gui.CustomFrame;
import com.finn.gui.Types;

public class LoginTerminal implements KeyListener {

CustomFrame frame = new CustomFrame(Types.TERMINAL);

JTextArea log = new JTextArea();
public JTextField field = new JTextField();

public void setVisible(boolean bool) {
    frame.setVisible(bool);
}

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}
public void timerPrint(String text) {
Timer timer = new Timer(50, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (index < text.length() - 1 && index >= 0) {
            String newChar = Character.toString(text.charAt(index));
            textArea.append(newChar);
            index++;
        } else {
            textArea.setText(null);
            index = 0;
           // You could stop the timer here...
        }
   }
});
timer.start();
}

public void slowPrint(String str) {
    for(int i = 0; i < str.length(); i++) {
        log.append("" + str.charAt(i));

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (i == str.length() - 1) {
            log.append("\n");
        }
    }
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

public LoginTerminal() {
    try {

        InputStream is = LoginTerminal.class.getResourceAsStream("/fonts/dungeon.TTF");
        Font font = Font.createFont(Font.TRUETYPE_FONT, is);
        font = font.deriveFont(Font.PLAIN, 10);

        frame.add(field);
        frame.add(log);

        log.setBackground(Color.BLACK);
        log.setForeground(Color.WHITE);
        log.setWrapStyleWord(true);
        log.setLineWrap(true);
        log.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        log.setFont(font);
        log.setEditable(false);
        log.setCaretColor(Color.BLACK);

        field.setBackground(Color.BLACK);
        field.setForeground(Color.WHITE);
        field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
        field.setFont(font);
        field.setCaretColor(Color.BLACK);
        field.addKeyListener(this);
        field.setText("  >  ");

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void dumpToLog() {
    log.append(field.getText() + "\n");
    field.setText("  >  ");
}

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ENTER && field.isFocusOwner()) {
        if(field.getText().equals("  >  HELP") || field.getText().equals("  >  help")) {
            dumpToLog();


        } else {
            dumpToLog();
        }
    }


    if(!field.getText().startsWith("  >  ")) {
        field.setText("  >  ");
    }
}

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

ANSWER

如果将来有人读到这篇文章并希望在Java中使用慢速打印,请使用以下命令:

int index;

public void timerPrint(final String text) {
    Timer timer = new Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < text.length() - 1 && index >= 0) {
                String newChar = Character.toString(text.charAt(index));
                textarea.append(newChar);
                index++;
            } else {
                index = 0;
                ((Timer)e.getSource()).stop();
            }
       }
    });
    timer.start();
}

1 个答案:

答案 0 :(得分:1)

Swing是一个单线程框架,也就是说,阻止事件调度线程的任何东西都会阻止UI更新或者程序响应新线程

有关详细信息,请参阅Concurrency in Swing

您永远不应该在EDT中执行任何阻止(Thread.sleep)或长时间运行的进程。

Swing也不是线程安全的,这意味着你永远不应该从EDT的上下文之外更新UI。

这让你陷入了一个难题......

幸运的是,有选择。对于您的情况,最简单的解决方案可能是使用Swing Timer,它可用于安排定期回调到EDT,您可以在其中执行更新

有关详细信息,请参阅How to use Swing Timers

例如......

Scrolling Text

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ScrollingText100 {

    public static void main(String[] args) {
        new ScrollingText100();
    }

    public ScrollingText100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private String text;
        private int index;
        private JTextArea textArea;

        public TestPane() {            
            setLayout(new BorderLayout());
            textArea = new JTextArea(2, 40);
            add(textArea);

            text = "Please login to continue...\n" + "Type 'help' for more information.\n";
            Timer timer = new Timer(50, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (index < text.length() - 1 && index >= 0) {
                        String newChar = Character.toString(text.charAt(index));
                        textArea.append(newChar);
                        index++;
                    } else {
                        textArea.setText(null);
                        index = 0;
                        // You could stop the timer here...
                    }
                }
            });
            timer.start();
        }

    }

}

<强>更新

如果我理解你的要求,就像这样......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LoginTerminal {

    private JTextArea log = new JTextArea(20, 40);
    private JTextField field = new JTextField();

    private int index;
    private StringBuilder textToDisplay;
    private Timer timer;

    public static void main(String[] args) {
        new LoginTerminal();
    }

    public LoginTerminal() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                textToDisplay = new StringBuilder(128);
                timer = new Timer(50, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (textToDisplay.length() > 0) {
                            String newChar = Character.toString(textToDisplay.charAt(0));
                            textToDisplay.delete(0, 1);
                            log.append(newChar);
                        } else {
                            ((Timer) e.getSource()).stop();
                        }
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(field, BorderLayout.NORTH);
                frame.add(new JScrollPane(log));

                log.setBackground(Color.BLACK);
                log.setForeground(Color.WHITE);
                log.setWrapStyleWord(true);
                log.setLineWrap(true);
                log.setEditable(false);
                log.setCaretColor(Color.BLACK);

                field.setBackground(Color.BLACK);
                field.setForeground(Color.WHITE);
                field.setCaretColor(Color.BLACK);
                field.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if ("  >  HELP".equalsIgnoreCase(field.getText())) {
                            dumpToLog();
                        } else {
                            dumpToLog();
                        }
                    }
                });
                field.setText("  >  ");

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

    public void timerPrint(String text) {

        textToDisplay.append(text).append("\n");
        if (!timer.isRunning()) {
            timer.start();
        }
    }

    public void slowPrint(String str) {
        for (int i = 0; i < str.length(); i++) {
            log.append("" + str.charAt(i));

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (i == str.length() - 1) {
                log.append("\n");
            }
        }
    }

    public void dumpToLog() {
        timerPrint(field.getText());
    }

}

可能就是你真正想要的东西。

注意,KeyListener不是JTextComponent的不错选择,在这种情况下,ActionListener是更好的选择。