这是我的jpanel,我在jframe中添加了它,但是每一个字母写一个字母的代码,但是当执行的时候冻结了,稍后,文本出现了,文本没有这样的效果,但是我认为它应该可以工作,为什么?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vista;
import java.awt.Image;
import javax.swing.ImageIcon;
import static paladins.nut.and.crush.panelver.PaladinsNutAndCrushPanelVer.mainPanel;
public class sceneKonami extends javax.swing.JPanel {
String ruta = "src/backgrounds/soniabelmont.png";
String ruta1 = "src/backgrounds/legends.png";
ImageIcon fondo = new ImageIcon(new ImageIcon(ruta).getImage().getScaledInstance(1280, 720, Image.SCALE_DEFAULT));
ImageIcon fondo1 = new ImageIcon(new ImageIcon(ruta1).getImage().getScaledInstance(730, 250, Image.SCALE_DEFAULT));
public sceneKonami() {
initComponents();
loader();
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
texter = new javax.swing.JLabel();
castlevania = new javax.swing.JLabel();
background = new javax.swing.JLabel();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 370, -1, -1));
add(texter, new org.netbeans.lib.awtextra.AbsoluteConstraints(184, 77, 1050, 510));
add(castlevania, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 30, 730, 250));
add(background, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1280, 720));
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String message = "Sonia Belmont is waiting, \nShe will appear soon. \nIn a great adventure";
slowPrint(message);
}
public static void slowPrint(String message) {
char[] chars = message.toCharArray();
for (int i = 0; i < chars.length; i++) {
texter.setText(texter.getText() + String.valueOf(message.charAt(i)));
mainPanel.repaint();
mainPanel.revalidate();
System.out.println(message.charAt(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Variables declaration - do not modify
private javax.swing.JLabel background;
private javax.swing.JLabel castlevania;
private javax.swing.JButton jButton1;
private static javax.swing.JLabel texter;
// End of variables declaration
private void loader() {
background.setIcon(fondo);
castlevania.setIcon(fondo1);
}
}
当程序运行时,我按下按钮运行类型writer eefect,程序被冻结,稍后文本出现
1条答案
按热度按时间9ceoxa921#
以下是您使用swing尝试实现的mre(1)
Timer
:(1) 在询问和回答时,始终考虑发布mre。