代码之家  ›  专栏  ›  技术社区  ›  ddalcanto

从字符串缓慢地将文本打印到jLabel?

  •  0
  • ddalcanto  · 技术社区  · 6 年前

    我尝试在jLabel中进行pokemon样式的打印,但由于它的限制,我无法成功地完成此操作。我能够完成这个任务,只需使用以下代码打印到控制台:

    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++;
            }
    

    然而,我随后遇到了文本的问题,没有通过循环添加上一次出现的内容。

    是否还有其他方法可以完成缓慢打印文本的任务?

    3 回复  |  直到 6 年前
        1
  •  2
  •   MadProgrammer    6 年前

    回转不是螺纹安全的,是单螺纹的。

    您不能阻塞事件调度线程,否则将不会绘制任何内容,也不应该从EDT上下文之外更新UI或UI依赖的任何内容。

    可运行示例

    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);
            }
    
        }
    
    }
    
        2
  •  0
  •   kai    6 年前

    我很快点击了一些与你需要的相似的东西:

    /**
     *
     * @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                   
    }
    
        3
  •  -2
  •   grooble    6 年前

    你不能把这个字符串放在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++;
            }