我试图做一个口袋妖怪式的打印一个JLabel里面,但由于其局限性,所以我一直无法成功地做到这一点。我能够完成这个任务简单地使用下面的代码打印到控制台:
for(int i = 0; i < test.length(); i++) {
System.out.print(test.charAt(i));
Thread.sleep(35);
}
然而,因为一个JLabel不能接受char数据类型,这种做法是行不通的。正因为如此,我尝试设置一个字符串,利用这一点,字符:
while (pos < text.length() - 1) {
char test = text.charAt(pos);
String print = String.valueOf(test);
label.setText(print);
try {
Thread.sleep(35);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
pos++;
}
然而,我再跑进不添加到期间通过环上一次是什么有文字的问题。
有另一种方式我可以做到的打印速度缓慢文字这个任务?
Swing不是线程安全的,是单线程的。
你不能阻止事件指派线程,否则没有东西画,你不应该更新UI或任何用户界面从EDT上下文之外依靠
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private String text = "All your base belong to us";
private int index;
private JLabel label = new JLabel("");
public TestPane() {
setLayout(new GridBagLayout());
add(label);
Timer timer = new Timer(35, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
label.setText(text.substring(0, index));
index++;
if (index > text.length()) {
((Timer) evt.getSource()).stop();
}
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
}
}
我赶紧点击共同的东西应该像你所需要的:
/**
*
* @author Kai
*/
public class Pokemon extends javax.swing.JFrame {
String text = "someTesttext";
StringBuffer current = new StringBuffer();
/**
* Creates new form Pokemon
*/
public Pokemon() {
initComponents();
new javax.swing.Timer(150, event -> {
if (current.length() == text.length()) {
current.setLength(0);
} else {
current.append(text.charAt(current.length()));
}
this.jLabel1.setText(current.toString());
}).start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(400, 400));
getContentPane().setLayout(null);
getContentPane().add(jLabel1);
jLabel1.setBounds(41, 23, 240, 30);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Pokemon().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
你不能只是把字符串while循环外面,然后添加到它。事情是这样的:
String print = ""; // add this line
while (pos < text.length() - 1) {
char test = text.charAt(pos);
print = print + String.valueOf(test); // add to the print
label.setText(print);
try {
Thread.sleep(35);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
pos++;
}