• JAVA 跑马灯文字效果


    JAVA跑马灯文字效果的实现:

    1. 首先创建一个继承JFrame类的HorseRaceLightTextFrame窗体类,代码如下:

    package com.example.horseracelighttext;
    
    import java.awt.Dimension;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import javax.swing.JFrame;
    
    public class HorseRaceLightTextFrame extends JFrame{
    
        /**
         * 
         */
        private static final long serialVersionUID = -1760723807752896879L;
        
        public HorseRaceLightTextFrame() {
            // TODO Auto-generated constructor stub
            this.addWindowListener(new WindowListener() {
                
                @Override
                public void windowOpened(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void windowIconified(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void windowDeiconified(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void windowDeactivated(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void windowClosing(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void windowClosed(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    System.exit(0);
                }
                
                @Override
                public void windowActivated(WindowEvent arg0) {
                    // TODO Auto-generated method stub
                    
                }
            });
            
            HorseRaceLightTextPanel panel = new HorseRaceLightTextPanel();
            this.add(panel);
            new Thread(panel).start();// 启动新线程
            this.setVisible(true);
        }
        
        public static void main(String[] args) {
            HorseRaceLightTextFrame frame = new HorseRaceLightTextFrame();
            frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            frame.setSize(360, 360);
        }
    
    }

    2. 在HorseRaceLightTextFrame窗体类中创建内部面板类,该面板类实现了Runnable接口,重写JComponent类中的paint()方法和实现Runnable类中的run()方法。在paint完成跑马灯文字的绘制;在run()方法中实现改变跑马灯文字的x坐标值。
    3. 将内部面板类HorseRaceLightTextPanel的实例添加到HorseRaceLightTextFrame窗口中,进行跑马灯文字效果的显示。内部面板类HorseRaceLightTextPanel代码如下:

    package com.example.horseracelighttext;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    /***
     * 跑马灯文字效果Panel
     * @author lenovo'
     *
     */
    public class HorseRaceLightTextPanel extends JPanel implements Runnable {
    
        String value = "这是一个文字跑马灯范例,谢谢!";   //需要绘制的文字
        char[] drawChar = value.toCharArray();
        int[] x = new int[drawChar.length];   //绘制每个字符的x坐标
        int y = 100; //绘制文字的y坐标
    
        @Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            g.clearRect(0, 0, getWidth(), getHeight());  //清除绘制前上下文内容
            Font font = new Font("华文楷体", Font.BOLD, 20);  //创建字体样式对象
            g.setFont(font);   //设置字体样式
            g.setColor(Color.RED);  //设置字体颜色
            for (int j = drawChar.length - 1; j >= 0; j--) {
                g.drawString(drawChar[drawChar.length - 1 - j] + "", x[j], y);  //绘制字符
            }
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
    
            try {
                boolean flag = false;  //为false表示第一次执行,x坐标进行等比递增,否则进行等差递增
                while (true) {
                    Thread.sleep(300);
                    for (int i = drawChar.length - 1; i >= 0; i--) {
                        if (!flag) {
                            x[i] = x[i] + 20 * i;  //x坐标进行等比递增
                        } else {
                            x[i] = x[i] + 20;   //x坐标进行等差递增
                        }
                        
                        if (x[i] >= 360 -20) {  //大于窗体宽度-20时,坐标值为0
                            x[i] = 0;   
                        }
                        
                    }
                    repaint();  //重新绘制图像
                    if (!flag) {
                        flag = true;
                    }
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }

    本文摘自:《JAVA开发实战1200例》---王家林

  • 相关阅读:
    BOOST 线程完全攻略
    BOOST 线程完全攻略
    BOOST 线程完全攻略
    BOOST 线程完全攻略
    Boost线程库学习笔记
    BOOST中如何实现线程安全代码
    多线程 AfxBeginThread 与 CreateThread 的区别
    AfxBeginThread的介绍/基本用法
    淘宝开源项目
    数据库中间件OneProxy and onemysql
  • 原文地址:https://www.cnblogs.com/tianyaxue/p/3178851.html
Copyright © 2020-2023  润新知