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();
	}

}

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class ZiMupanel extends JFrame { public ZiMupanel() { Container con = this.getContentPane(); // 设置窗口名称 this.setTitle("滚动字幕"); // 设置布局管理器为 null ,方便 面板定位,一般使用null 布局时,将窗口设置不可改变大小 this.setLayout(null); // 设置窗口位置和大小 this.setBounds(300, 300, 460, 330); // 设置窗口可见 this.setVisible(true); // 设置不可改变大小 this.setResizable(false); // 设置当点击窗口的关闭按钮时退出 // 设置此窗体关闭,滚动面板不关闭 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Panelfont1 p1 = new Panelfont1(); con.add(p1); p1.setBounds(15, 15, 175, 60); Panelfont2 p2 = new Panelfont2(); con.add(p2); p2.setBounds(195, 15, 250, 60); Panelneizi p3 = new Panelneizi(); con.add(p3); p3.setBounds(15, 80, 175, 80); Paneldirection p4 = new Paneldirection(); con.add(p4); p4.setBounds(195, 80, 250, 80); Panel5 p5 = new Panel5(); con.add(p5); p5.setBounds(15, 155, 430, 70); Panel6 p6 = new Panel6(); con.add(p6); p6.setBounds(15, 220, 430, 70); } } class Panelfont1 extends JPanel implements ActionListener{ public Panelfont1() { this.setBorder(BorderFactory.createTitledBorder("滚动字幕的文字大小控制"));//设置 面板边框 this.setLayout(new GridLayout(1, 2)); bigfont = new JRadioButton("大字体", false); smallfont = new JRadioButton("小字体", true); bigfont.addActionListener(this); smallfont.addActionListener(this); ButtonGroup sexRadioButtonGroup = new ButtonGroup();// 创建一个选按钮组 sexRadioButtonGroup.add(bigfont);// 将单选按钮对象添加到按钮组中 sexRadioButtonGroup.add(smallfont);// 将单选按钮对象添加到按钮组中 //Enumeration elements = sexRadioButtonGroup.getElements();// 遍历按钮组中的所有按钮 this.add(bigfont); this.add(smallfont); } public void actionPerformed(ActionEvent e) { if(bigfont.isSelected()){ fontSize = 50; }else { fontSize = 36; } Font font = new Font(fontName,Font.BOLD,fontSize);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值