版主,可以给个实现从左到右滚动,或从右到左滚动的代码啊??
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);
}
}
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);
}
}