jlable实现从上往下滚动。有需要的朋友拿去吧 代码写的很烂,希望不要拍砖
package UI;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 带有滚动效果的Label标签,可继续拓展很多特效,例如颜色变换、速度变换等
*/
public 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 y = 0;
private Vector v;
private Vector v2;
int j=0;
private int w = 0, h = 0;
public MoveLabel(String text) {
super(text);
this.text = text;
thread = new Thread(this);
v = new Vector();
v2 = new Vector();
addMouseListener(new MouseAdapter() {
public void mouseEntered(final MouseEvent arg0)
{
setCursor(new Cursor(Cursor.HAND_CURSOR));
setForeground(new Color(0, 0, 255));
setText("<html><font><U>"+ getText()+"</U></font></html>");
}
public void mouseExited(final MouseEvent arg0)
{
try
{
setForeground(new Color(0, 0, 0));
String str = getText();
str=str.substring(15,str.length());
str=str.substring(0,str.length()-18);
setText(str);
}
catch(Exception e)
{
setText("<html><font><U>"+ getText()+"</U></font></html>");
}
}
public void mouseClicked(final MouseEvent arg0)
{
String str = getText();
System.out.println(str);
int j = 0;
for(int i=0;i<v.size();i++)
{
String content = v.get(i).toString();
if(str.contains(content))
{
j=i;
break;
}
}
//调用默认浏览器
String cmmd ="rundll32 url.dll FileProtocolHandler ";
String url = v2.get(j).toString();
Runtime rt=Runtime.getRuntime();
try
{
rt.exec(cmmd + url);
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
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, y);
}
public void run() {
while (true) {
y -= 1;
if (y == h/2) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (y < 0) {
if(j>=v.size()) j=0;
setText(v.get(j).toString());
y = h+10;
j++;
}
this.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void addText(String text,String superLink)
{
v.add(text);
v2.add(superLink);
}
public void addText(String text)
{
v.add(text);
}
public void addLink(String superLink)
{
v2.add(superLink);
}
public void removeItem(int index)
{
v.remove(index);
v2.remove(index);
}
public void removeAllItems()
{
v.clear();
v2.clear();
}
}
测试类
package UI;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class testt extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testt frame = new testt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public testt() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(151, 100, 178, 74);
getContentPane().add(panel);
final MoveLabel label = new MoveLabel("2312");
label.setBounds(59, 22, 66, 18);
label.addText("baidu");
label.addLink("www.baidu.com");
label.addText("sina");
label.addLink("sina.com");
label.addText("sougou");
label.addLink("sougou.com");
panel.add(label);
//
}
}