当前位置: 代码迷 >> Java相关 >> 滚动代码
  详细解决方案

滚动代码

热度:160   发布时间:2007-01-05 19:00:06.0
滚动代码

版主,可以给个实现从左到右滚动,或从右到左滚动的代码啊??

搜索更多相关的解决方案: 代码  滚动  

----------------解决方案--------------------------------------------------------
用线程实现
----------------解决方案--------------------------------------------------------
嘻嘻路过!~~从左到右,从上到下!~~~
----------------解决方案--------------------------------------------------------

版主,可以给个代码不啊。我线程学的不是很好啊


----------------解决方案--------------------------------------------------------

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

public class ScrollText extends JPanel implements Runnable{
private volatile int currOffset;
private int width;
private Thread runThread;
private volatile boolean isRun;

public ScrollText(int width){
currOffset=0;
this.width=width;
isRun=true;

runThread=new Thread(this,"ScrollText");
runThread.start();
}

public void run(){
while(isRun){
try{
Thread.sleep(300);
currOffset+=10;
if(currOffset>=width){
currOffset=0;
}
repaint();
}catch(Exception ex){
Thread.currentThread().interrupt();
}
}
}

public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawString("java my love",currOffset,50);
}

}


----------------解决方案--------------------------------------------------------
不错,
----------------解决方案--------------------------------------------------------

版主,做的我真是太累拉,实在是想不出拉,帮我把下面的一行字实现从左到右滚动啊.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
import java.text.SimpleDateFormat;

public class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp;
public Clock(){
Thread c=new Thread(this);
c.start();
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER);

}

public void run(){
while(true){
this.repaint();
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}

}

}

public void paint(Graphics g){
super.paint(g);
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE MM月dd日 yyyy年 HH:mm:ss");
Date date=new Date() ;
jt.setText((bartDateFormat.format(date)));
}

public static void main(String args[]){
Clock cd=new Clock();
JFrame j=new JFrame("我的时钟");
j.getContentPane().add(cd);
j.setBounds(200,200,400,400);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



----------------解决方案--------------------------------------------------------

版主,做的我真是太累拉,实在是想不出拉,帮我把下面的一行字实现从左到右滚动啊.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
import java.text.SimpleDateFormat;

public class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp;
public Clock(){
Thread c=new Thread(this);
c.start();
jp=new JPanel();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);
add(jp,BorderLayout.CENTER);

}

public void run(){
while(true){
this.repaint();
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}

}

}

public void paint(Graphics g){
super.paint(g);
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE MM月dd日 yyyy年 HH:mm:ss");
Date date=new Date() ;
jt.setText((bartDateFormat.format(date)));
}

public static void main(String args[]){
Clock cd=new Clock();
JFrame j=new JFrame("我的时钟");
j.getContentPane().add(cd);
j.setBounds(200,200,400,400);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



----------------解决方案--------------------------------------------------------
既把 显示的" 星期五 1月05日 2007年 12:12:25"实现从左到右滚动啊,麻烦版主拉
----------------解决方案--------------------------------------------------------

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
import java.text.SimpleDateFormat;

public class Clock extends JPanel implements Runnable{
private JTextField jt;
private JPanel jp;
private String date="";
private int x;
private SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE MM月dd日 yyyy年 HH:mm:ss");
public Clock(){
Thread c=new Thread(this);
c.start();
jt=new JTextField();
jt.setEditable(false);
jt.setHorizontalAlignment(jt.CENTER);
this.setLayout(new BorderLayout());
add(jt,BorderLayout.SOUTH);

}

public void run(){
while(true){
try{
Thread.sleep(50);
date=bartDateFormat.format(new Date());
x+=2;
if(x>this.getWidth()){
x=0;
}
repaint();
} catch(Exception e){
e.printStackTrace();
}

}

}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(date,x,100);
Date date=new Date() ;
jt.setText((bartDateFormat.format(date)));
}

public static void main(String args[]){
Clock cd=new Clock();
JFrame j=new JFrame("我的时钟");
j.getContentPane().add(cd);
j.setBounds(200,200,400,400);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


----------------解决方案--------------------------------------------------------
  相关解决方案