Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mouamle.animation;
- import java.awt.EventQueue;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- import mouamle.uilib.Frame;
- import mouamle.uilib.Label;
- import mouamle.uilib.TextBox;
- public class AnimatedText implements Runnable{
- private JFrame frmAnimatedText;
- private JTextField textField;
- private JLabel lblNewLabel;
- private TextBox textBox;
- private String text;
- private long delay;
- Thread t;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- AnimatedText window = new AnimatedText();
- window.frmAnimatedText.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public AnimatedText() {
- initialize();
- }
- private void initialize() {
- frmAnimatedText = new Frame();
- frmAnimatedText.setTitle("Animated text");
- frmAnimatedText.setBounds(100, 100, 450, 300);
- frmAnimatedText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JButton btnNewButton = new JButton("Animate");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- text = textField.getText();
- delay = Long.parseLong(textBox.getText());
- animateText();
- }
- });
- btnNewButton.setBounds(10, 211, 424, 50);
- frmAnimatedText.getContentPane().add(btnNewButton);
- lblNewLabel = new Label(" ");
- lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
- lblNewLabel.setText(" ");
- lblNewLabel.setBounds(10, 11, 424, 50);
- frmAnimatedText.getContentPane().add(lblNewLabel);
- textField = new TextBox("DARK_GRAY");
- textField.setText("Mouamle");
- textField.setHorizontalAlignment(SwingConstants.CENTER);
- textField.setFont(new Font("Tahoma", Font.PLAIN, 35));
- textField.setBounds(10, 67, 424, 50);
- frmAnimatedText.getContentPane().add(textField);
- textField.setColumns(10);
- textBox = new TextBox("DARK_GRAY");
- textBox.setHorizontalAlignment(SwingConstants.CENTER);
- textBox.setText("120");
- textBox.setToolTipText("Time");
- textBox.setFont(new Font("Tahoma", Font.BOLD, 39));
- textBox.setColumns(10);
- textBox.setBounds(118, 128, 214, 50);
- frmAnimatedText.getContentPane().add(textBox);
- }
- public void animateText(){
- t = new Thread(this);
- t.start();
- }
- @Override
- public void run() {
- if ( !lblNewLabel.getText().equalsIgnoreCase(" ")){
- lblNewLabel.setText(" ");
- }
- char[] c = text.toCharArray();
- for (char d : c) {
- lblNewLabel.setText(lblNewLabel.getText() + d);
- try {
- Thread.sleep(delay);
- } catch (InterruptedException e) {}
- }
- t.interrupt();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement