swing中公告栏的信息怎么滚动???

wwq101693 2009-01-03 10:52:59
我现在要做一个公告栏,例如:公告:公司今天下午2点开会。然后,人家要求“公司今天下午2点开会。”这些字要滚动的!怎么才能让他滚动开啊,大家帮帮忙
...全文
255 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bzwm 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gongfuliang 的回复:]
引用 4 楼 neo0208 的回复:
自己再加多个双缓冲处理 就行了!

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SwingDemo extends JFrame {

public SwingDemo() {
this.setLayout(new Bor…
[/Quote]

javax.swing.Timer 这个可以。
Dream_JavaWorld 2009-01-12
  • 打赏
  • 举报
回复
mark
nj_dobetter 2009-01-09
  • 打赏
  • 举报
回复
用线程。
chen_2001 2009-01-09
  • 打赏
  • 举报
回复
写个线程
让它的字符串滚动

import java.awt.*;
import javax.swing.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class MoveLabel
extends JLabel implements Runnable {

private String text = null;

private Thread thread = null;

private int x = 0;
private int strWidth = 0;

private int w = 0, h = 0;
Font font = new Font("MS Sans Serif", Font.PLAIN, 12);
FontMetrics fm = this.getFontMetrics(font);

public MoveLabel(String text) {
super(text);
this.text = text;
thread = new Thread(this);
thread.start();
}

public String getText() {
return text;
}

public void setText2Label(String text) {
super.setText(text);
this.text = text;
strWidth = fm.stringWidth(text);
x = w;
}

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

public void run() {

if (x + strWidth < 0) {
x = w;
}
if (x + strWidth >= 0) {
x -= 2;
}
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}

}
likgui 2009-01-08
  • 打赏
  • 举报
回复
关注中
wwq101693 2009-01-06
  • 打赏
  • 举报
回复
neo0208,大哥,缓冲区你也给写一下吧,多谢了,明天就交活了,帮帮忙,在线等了........
贝壳鱼 2009-01-06
  • 打赏
  • 举报
回复
draw string 在 一张 image 上, 然后 setClip 然后 循环 move.
gongfuliang 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 neo0208 的回复:]
自己再加多个双缓冲处理 就行了!

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SwingDemo extends JFrame {

public SwingDemo() {
this.setLayout(new BorderLayout…
[/Quote]

呵呵,配合的还挺好的,不过Timer用错包了,用util包下的
neo0208 2009-01-05
  • 打赏
  • 举报
回复
自己再加多个双缓冲处理 就行了!

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SwingDemo extends JFrame {

public SwingDemo() {
this.setLayout(new BorderLayout());
this.add(new MyJPanel("I hate you"), BorderLayout.CENTER);
this.setBounds(200, 100, 600, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
new SwingDemo();
}

class MyJPanel extends JPanel {
private String message = "";
private int index = 50;

public MyJPanel(String message) {
this.message = message;
refreshTimer.start();
}

Timer refreshTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
refreshTimer_actionPerformed();
}
});

public void paint(Graphics g) {
Graphics2D gg = (Graphics2D)g;
gg.drawString(message, index, 50);
}

protected void refreshTimer_actionPerformed() {
index = index + 2;
if (index >= 100) {
index = 50;
}
this.repaint();
}
}
}
wwq101693 2009-01-04
  • 打赏
  • 举报
回复
二楼的兄弟能说的详细点吗,写点伪代码也行,小弟刚接触swing,还不怎么了解!
gongfuliang 2009-01-03
  • 打赏
  • 举报
回复
paint就行了。

重写paintComponent方法 over

drawString(s,x,y),根据你的需要设置x的值就行了,使用Timer刷新
dudenglan 2009-01-03
  • 打赏
  • 举报
回复
用一个JLABLE加上线程就可以了

62,614

社区成员

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

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