在一个JLabel中实现打字机效果。

huangapple go评论176阅读模式
英文:

Make a typewriter effect in a jlabel

问题

这是我的JPanel,我将其添加到JFrame中,但是写入每个字母的代码在执行时会冻结,过了一段时间后文本才会出现,文本没有以那种效果进行书写,但我认为它应该能正常工作,为什么?

  1. /*
  2. * 更改许可证头,选择项目属性中的许可证头。
  3. * 更改模板文件,选择工具|模板
  4. * 打开编辑器中的模板并打开模板
  5. */
  6. package vista;
  7. import java.awt.Image;
  8. import javax.swing.ImageIcon;
  9. import static paladins.nut.and.crush.panelver.PaladinsNutAndCrushPanelVer.mainPanel;
  10. public class sceneKonami extends javax.swing.JPanel {
  11. String ruta = "src/backgrounds/soniabelmont.png";
  12. String ruta1 = "src/backgrounds/legends.png";
  13. ImageIcon fondo = new ImageIcon(new ImageIcon(ruta).getImage().getScaledInstance(1280, 720, Image.SCALE_DEFAULT));
  14. ImageIcon fondo1 = new ImageIcon(new ImageIcon(ruta1).getImage().getScaledInstance(730, 250, Image.SCALE_DEFAULT));
  15. public sceneKonami() {
  16. initComponents();
  17. loader();
  18. }
  19. /**
  20. * This method is called from within the constructor to initialize the form.
  21. * WARNING: Do NOT modify this code. The content of this method is always
  22. * regenerated by the Form Editor.
  23. */
  24. @SuppressWarnings("unchecked")
  25. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  26. private void initComponents() {
  27. jButton1 = new javax.swing.JButton();
  28. texter = new javax.swing.JLabel();
  29. castlevania = new javax.swing.JLabel();
  30. background = new javax.swing.JLabel();
  31. setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  32. jButton1.setText("jButton1");
  33. jButton1.addActionListener(new java.awt.event.ActionListener() {
  34. public void actionPerformed(java.awt.event.ActionEvent evt) {
  35. jButton1ActionPerformed(evt);
  36. }
  37. });
  38. add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 370, -1, -1));
  39. add(texter, new org.netbeans.lib.awtextra.AbsoluteConstraints(184, 77, 1050, 510));
  40. add(castlevania, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 30, 730, 250));
  41. add(background, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1280, 720));
  42. }// </editor-fold>
  43. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  44. String message = "Sonia Belmont is waiting, \nShe will appear soon. \nIn a great adventure";
  45. slowPrint(message);
  46. }
  47. public static void slowPrint(String message) {
  48. char[] chars = message.toCharArray();
  49. for (int i = 0; i < chars.length; i++) {
  50. texter.setText(texter.getText() + String.valueOf(message.charAt(i)));
  51. mainPanel.repaint();
  52. mainPanel.revalidate();
  53. System.out.println(message.charAt(i));
  54. try {
  55. Thread.sleep(100);
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. // Variables declaration - do not modify
  62. private javax.swing.JLabel background;
  63. private javax.swing.JLabel castlevania;
  64. private javax.swing.JButton jButton1;
  65. private static javax.swing.JLabel texter;
  66. // End of variables declaration
  67. private void loader() {
  68. background.setIcon(fondo);
  69. castlevania.setIcon(fondo1);
  70. }
  71. }

当程序运行并且我按下按钮来运行打字机效果时,程序会冻结,过了一段时间后文本才会出现。

英文:

This is my JPanel which I'm adding in a JFrame, but the code that write letter per letter but when executed freezes and sometime later, the text appears, the text is not writing with that effect, but I think it should be working, why?

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package vista;
  7. import java.awt.Image;
  8. import javax.swing.ImageIcon;
  9. import static paladins.nut.and.crush.panelver.PaladinsNutAndCrushPanelVer.mainPanel;
  10. public class sceneKonami extends javax.swing.JPanel {
  11. String ruta = &quot;src/backgrounds/soniabelmont.png&quot;;
  12. String ruta1 = &quot;src/backgrounds/legends.png&quot;;
  13. ImageIcon fondo = new ImageIcon(new ImageIcon(ruta).getImage().getScaledInstance(1280, 720, Image.SCALE_DEFAULT));
  14. ImageIcon fondo1 = new ImageIcon(new ImageIcon(ruta1).getImage().getScaledInstance(730, 250, Image.SCALE_DEFAULT));
  15. public sceneKonami() {
  16. initComponents();
  17. loader();
  18. }
  19. /**
  20. * This method is called from within the constructor to initialize the form.
  21. * WARNING: Do NOT modify this code. The content of this method is always
  22. * regenerated by the Form Editor.
  23. */
  24. @SuppressWarnings(&quot;unchecked&quot;)
  25. // &lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot;Generated Code&quot;&gt;
  26. private void initComponents() {
  27. jButton1 = new javax.swing.JButton();
  28. texter = new javax.swing.JLabel();
  29. castlevania = new javax.swing.JLabel();
  30. background = new javax.swing.JLabel();
  31. setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  32. jButton1.setText(&quot;jButton1&quot;);
  33. jButton1.addActionListener(new java.awt.event.ActionListener() {
  34. public void actionPerformed(java.awt.event.ActionEvent evt) {
  35. jButton1ActionPerformed(evt);
  36. }
  37. });
  38. add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 370, -1, -1));
  39. add(texter, new org.netbeans.lib.awtextra.AbsoluteConstraints(184, 77, 1050, 510));
  40. add(castlevania, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 30, 730, 250));
  41. add(background, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1280, 720));
  42. }// &lt;/editor-fold&gt;
  43. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  44. String message = &quot;Sonia Belmont is waiting, \nShe will appear soon. \nIn a great adventure&quot;;
  45. slowPrint(message);
  46. }
  47. public static void slowPrint(String message) {
  48. char[] chars = message.toCharArray();
  49. for (int i = 0; i &lt; chars.length; i++) {
  50. texter.setText(texter.getText() + String.valueOf(message.charAt(i)));
  51. mainPanel.repaint();
  52. mainPanel.revalidate();
  53. System.out.println(message.charAt(i));
  54. try {
  55. Thread.sleep(100);
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. // Variables declaration - do not modify
  62. private javax.swing.JLabel background;
  63. private javax.swing.JLabel castlevania;
  64. private javax.swing.JButton jButton1;
  65. private static javax.swing.JLabel texter;
  66. // End of variables declaration
  67. private void loader() {
  68. background.setIcon(fondo);
  69. castlevania.setIcon(fondo1);
  70. }
  71. }

when the program run and i press the buttom to run the type writter eefect, the program got freeze and sometime later the text appears

答案1

得分: 0

  1. 以下是您尝试使用 Swing `Timer` 实现的 [mre](https://stackoverflow.com/help/minimal-reproducible-example) &lt;sup&gt;(1)&lt;/sup&gt;:
  2. ```java
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.event.ActionEvent;
  6. import javax.swing.AbstractAction;
  7. import javax.swing.JFrame;
  8. public class SwingMain {
  9. SwingMain() {
  10. JFrame frame = new JFrame();
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. frame.setLocationRelativeTo(null);
  13. frame.add(new SceneKonami());
  14. frame.pack();
  15. frame.setVisible(true);
  16. }
  17. public static void main(String[] args) {
  18. new SwingMain();
  19. }
  20. }
  21. class SceneKonami extends javax.swing.JPanel {
  22. private int index = 0;
  23. private javax.swing.Timer timer;
  24. private javax.swing.JButton jButton1;
  25. private javax.swing.JLabel texter;
  26. public SceneKonami() {
  27. initComponents();
  28. }
  29. private void initComponents() {
  30. jButton1 = new javax.swing.JButton();
  31. texter = new javax.swing.JLabel();
  32. setLayout(new BorderLayout(5, 5));
  33. jButton1.setText("Type Message");
  34. jButton1.addActionListener(evt -> jButton1ActionPerformed(evt));
  35. add(jButton1, BorderLayout.SOUTH);
  36. add(texter, BorderLayout.NORTH);
  37. setPreferredSize(new Dimension(400, 150));
  38. }
  39. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  40. String message = "Sonia Belmont is waiting, \nShe will appear soon. \nIn a great adventure";
  41. slowPrint(message);
  42. }
  43. public void slowPrint(String message) {
  44. if (timer != null && timer.isRunning()) return;
  45. index = 0;
  46. texter.setText("");
  47. timer = new javax.swing.Timer(100, new AbstractAction() {
  48. @Override
  49. public void actionPerformed(ActionEvent e) {
  50. texter.setText(texter.getText() + String.valueOf(message.charAt(index)));
  51. index++;
  52. if (index >= message.length()) {
  53. timer.stop();
  54. }
  55. }
  56. });
  57. timer.start();
  58. }
  59. }

<hr>
<sup>(1)</sup> 在提问和回答时,始终考虑发布一个最小可复现示例(MRE)。

英文:

The following is mre <sup>(1)</sup> of what you are trying to achieve, using a swing Timer:

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.AbstractAction;
  5. import javax.swing.JFrame;
  6. public class SwingMain {
  7. SwingMain() {
  8. JFrame frame = new JFrame();
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. frame.setLocationRelativeTo(null);
  11. frame.add(new SceneKonami());
  12. frame.pack();
  13. frame.setVisible(true);
  14. }
  15. public static void main(String[] args) {
  16. new SwingMain();
  17. }
  18. }
  19. class SceneKonami extends javax.swing.JPanel {
  20. private int index = 0;
  21. private javax.swing.Timer timer;
  22. private javax.swing.JButton jButton1;
  23. private javax.swing.JLabel texter;
  24. public SceneKonami() {
  25. initComponents();
  26. }
  27. private void initComponents() {
  28. jButton1 = new javax.swing.JButton();
  29. texter = new javax.swing.JLabel();
  30. setLayout(new BorderLayout(5,5));
  31. jButton1.setText(&quot;Type Message&quot;);
  32. jButton1.addActionListener(evt -&gt; jButton1ActionPerformed(evt));
  33. add(jButton1, BorderLayout.SOUTH);
  34. add(texter, BorderLayout.NORTH);
  35. setPreferredSize(new Dimension(400,150));
  36. }
  37. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  38. String message = &quot;Sonia Belmont is waiting, \nShe will appear soon. \nIn a great adventure&quot;;
  39. slowPrint(message);
  40. }
  41. public void slowPrint(String message) {
  42. if(timer != null &amp;&amp; timer.isRunning()) return;
  43. index = 0;
  44. texter.setText(&quot;&quot;);
  45. timer = new javax.swing.Timer(100,new AbstractAction() {
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. texter.setText(texter.getText() + String.valueOf(message.charAt(index)));
  49. index++;
  50. if (index &gt;= message.length()) {
  51. timer.stop();
  52. }
  53. }
  54. });
  55. timer.start();
  56. }
  57. }

<hr>
<sup>(1)</sup> Always consider posting an MRE when asking and answering.

huangapple
  • 本文由 发表于 2020年8月19日 02:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63474574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定