Java swing 实现字体滚动

Java swing 实现字体滚动,swing跑马灯主要的技术便是线程,利用线程控制组件上的文字滚动。

swing自定义组件,实现字体滚动:

import java.awt.Graphics;
import java.util.concurrent.ExecutorService;

import javax.swing.JButton;
import javax.swing.JLabel;


/** * 显示界面的组件实体类 * @author 崔耀强 20150507 * @version 1.1 * */

public class MyComponent extends JButton {

    private static final long serialVersionUID = 1L;
    private JLabel jl2;//放value
    private String[] msg;

    private int x=90,y=17;
    private int i=0;
    private Roll roll;
    private ExecutorService pool;
    public static boolean flag;



    public JLabel getJl2() {
        return jl2;
    }

    public void setJl2(JLabel jl2) {
        this.jl2 = jl2;
    }

    public MyComponent() {
        super();
        this.setLayout(null);
        JLabel jl2=new JLabel();
        this.jl2=jl2;
        jl2.setBounds(0, 0, 100, 30);
        this.add(jl2);
    }

    /** * 设置显示值 * */
    public void setArrText(String[] msg){
        this.msg=msg;
        /* * 滚动实现 * */
        if(msg!=null&&msg.length>0){
            flag=true;
            pool=ThreadGroupUtil.getInstance();
            if(roll==null){
                roll=new Roll();
                // roll.start();
                pool.execute(roll);
            } 
        }else{
            flag=false;
        }
    }

    /** * 滚动线程,当有业务值传进来时候,启动该线程 */
    class Roll extends Thread{
        @Override
        public void run(){ 
            while(flag){
                try {
                    Thread.sleep(100);
                    if(jl2.getText()!=null){
                        jl2.setText(null);
                    }
                } catch (InterruptedException e) {
                }
                if((x-=1) > -25){
                    continue;
                } 
                i = ++i % msg.length;

                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                }
                x=90;
            }
        }
    } 
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(msg!=null){
            if(i<msg.length){

                g.drawString(msg[i],x,y);
            }
            this.repaint();
        }
    }
}

工具类,管理线程



import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 单例实现线程池类,管理组件滚动产生的线程 * @author 崔耀强 * @version 1.1 * */ public class ThreadGroupUtil { private static ExecutorService pool = null; private static int size; static { size=400; System.out.println(size); pool = Executors. newFixedThreadPool(size); } /** * 得到单例的线程池,线程池有大小,大小为设备组件总数的二倍 * @return ExecutorService 线程池 */ public static ExecutorService getInstance() { return pool; } } 

测试类

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestRoll extends JFrame{

    /** * @param args */
    public TestRoll() {

        JPanel jp=new JPanel();
        jp.setLayout(null);
        MyComponent component=new MyComponent();
        component.setBounds(20,30, 100, 30);
        jp.add(component);
        component.setArrText(new String []{"张三","李四","王五"});
        this.getContentPane().add(jp);
        this.setSize(300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestRoll();
    }

}

你可能感兴趣的:(java,swing,文字滚动)