java jlabel图片滚动条_Java基于线程实现带有滚动效果的Label标签实例

本文展示了一个Java程序,通过继承JFrame和JLabel,创建了一个MoveLabel类,实现了一个带有滚动效果的标签。MoveLabel内部启动一个线程,不断更新标签的位置,从而实现文字的滚动。可以通过调整线程睡眠时间和位置更新幅度来改变滚动速度和效果。
摘要由CSDN通过智能技术生成

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

/**

* Java中用线程实现带有滚动效果的Label标签

*/

public class Test extends JFrame {

private static final long serialVersionUID = -2397593626990759111L;

private JPanel pane = null;

private MoveLabel label = null;

public Test() {

super("Test");

pane = new JPanel();

label = new MoveLabel("带有滚动效果的标签");

pane.add(label);

this.getContentPane().add(pane);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(300, 200);

this.setVisible(true);

}

public static void main(String args[]) {

new Test();

}

/**

* 带有滚动效果的Label标签,可继续拓展很多特效,例如颜色变换、速度变换等

*/

private class MoveLabel extends JLabel implements Runnable {

private static final long serialVersionUID = 1891684760189602720L;

private String text = null;

private Thread thread = null;

private int x = 0;

private int w = 0, h = 0;

public MoveLabel(String text) {

super(text);

this.text = text;

thread = new Thread(this);

thread.start();

}

public String getText() {

return text;

}

public void setText(String text) {

super.setText(text);

this.text = text;

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(this.getBackground());

g.fillRect(0, 0, w = this.getWidth(), h = this.getHeight());

g.setColor(this.getForeground());

g.setFont(this.getFont());

g.drawString(text, x, h - 2);

}

public void run() {

while (true) {

x -= 2;

if (x < -w) {

x = w;

}

this.repaint();

try {

Thread.sleep(50);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

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、付费专栏及课程。

余额充值