Swing,如何在JLabel上实现滚动字符???

nicholasmars 2008-03-15 05:02:56
在Swing中用一个JLabel保存一个String,让这个String滚动起来,不会做。。。。。。
希望大家帮帮忙,最好要有代码。。。。
...全文
253 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicholasmars 2008-03-16
  • 打赏
  • 举报
回复
好人多啊~~~
Inhibitory 2008-03-15
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;

public class Test extends JPanel {
private static final long serialVersionUID = 4767050156491994899L;
private JLabel label;
private String scrolledText;

public Test() {
scrolledText = "滚动文字 Demo";
label = new JLabel(new String(scrolledText));
this.add(label);

Thread thread = new Thread(new TextChanger(label));
thread.start();
}

// @Override
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// Graphics2D g2d = (Graphics2D) g;
//
// }

private static void createAndShowGUI() {
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);

frame.getContentPane().add(new Test());

frame.setVisible(true);
}

public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test.createAndShowGUI();
}
});
}
}

class TextChanger implements Runnable {
private JLabel label;

public TextChanger(JLabel label) {
this.label = label;
}

public void run() {
try {
while (true) {
String text = label.getText();
if (text.length() > 1) {
text = text.substring(1, text.length()) + text.charAt(0);
label.setText(text);

// Get the frame
Component frame = SwingUtilities.getRoot(label);
if (frame != null && (frame instanceof JFrame)) {
((JFrame)frame).setTitle(text);
}

label.repaint();
}

Thread.sleep(300);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
awusoft 2008-03-15
  • 打赏
  • 举报
回复
呵呵,我不知道什么是EDT纯程哦。有时间去看看
lping1986 2008-03-15
  • 打赏
  • 举报
回复
学习
insiku 2008-03-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 AWUSOFT 的回复:]
[code=JAVA]
import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingTest extends JFrame{
private JLabel lable = new JLabel("滚动的文字");
public SwingTest()
{
this.setBackground(Color.BLACK);
this.getContentPane().setBackground(Color.blue);
this.setSize(200,300);
this.setVisible(true);
lable.setBa…
[/Quote]

你的代码虽然可以用
但是 是不安全的
刷新界面的线程必须放在EDT线程中去做
nicholasmars 2008-03-15
  • 打赏
  • 举报
回复
谢谢
awusoft 2008-03-15
  • 打赏
  • 举报
回复
[code=JAVA]
import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingTest extends JFrame{
private JLabel lable = new JLabel("滚动的文字");
public SwingTest()
{
this.setBackground(Color.BLACK);
this.getContentPane().setBackground(Color.blue);
this.setSize(200,300);
this.setVisible(true);
lable.setBackground(Color.red);
lable.setSize(200, 50);
this.getContentPane().add(lable);
new ChangeCaption(lable).start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JLabel getLable()
{
return lable;
}
public static void main(String[]args)
{
new SwingTest();
}

}
class ChangeCaption extends Thread
{
private JLabel lable;
public ChangeCaption(JLabel lable)
{
this.lable = lable;
}
public void run() {
// TODO Auto-generated method stub
//super.run();
while(true)
{
String text = lable.getText();
lable.setText(text.substring(1,text.length())+text.substring(0,1));
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


[/code]
awusoft 2008-03-15
  • 打赏
  • 举报
回复
流动的原理就是,"JAVA"下一次变成"AVAJ" 再一下次就是变成"VAJA",再下来就是"AJAV"
用一个线程去做,让这个lable的action = action.substring(1,action.length)+action.substring(0,1);
延迟一定时间.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧