g.drawString(msg,50,30);这里坐标是固定的,能滚动么?

解决方案 »

  1.   

    既然是通过绘图(paint)绘文字,而且要滚动,所以,g.drawString是,把坐标变化啊!比如g.drawString(msg,x,30);然后,在某地方改动x,然后通知repaint()重绘.
      

  2.   

    import java.awt.*;
    import java.applet.*;public class applet1 extends Applet implements Runnable{
      Thread t;
      String msg="welcome to java!";
      int state=70;
      boolean stopflag;  public void init(){
        setBackground(Color.cyan);
        setForeground(Color.yellow);
      }  public void start(){
      t=new Thread(this);
      stopflag=false;
      t.start();
      }   public void run()
             {
              
              while(true){
              state = state -1;
              if (state< -69)
                  state=70;
              repaint();
              try{
                  Thread.sleep(300);
                  }catch(InterruptedException e){}
              }
          }       public void stop(){ 
                  stopflag=true;
                  t=null;
                 }   public void paint(Graphics g)
              {
               for(int i=0; i<=msg.length(); i++)
                   g.drawString(msg.charAt(i),state+i,50) //state+i 依次绘出单个字符
                                                          //行固定不变     
              }
            由于我在网吧上网没有调试环境 所以这个程序我并没有测试 其中可能存在一起错误,不过基本是这个思路 我会继续关注 有不清楚的给我回复 
      

  3.   

    import java.awt.*;
    import java.applet.*;public class applet1 extends Applet implements Runnable{
      Thread t;
      String msg="welcome to java!";
      int state=70;
      boolean stopflag;  public void init(){
        setBackground(Color.cyan);
        setForeground(Color.yellow);
      }  public void start(){
      t=new Thread(this);
      stopflag=false;
      t.start();
      }   public void run()
             {
              
              while(true){
              state = state -1;
              if (state< -69)
                  state=70;
              repaint();
              try{
                  Thread.sleep(300);
                  }catch(InterruptedException e){}
              }
          }       public void stop(){ 
                  stopflag=true;
                  t=null;
                 }   public void paint(Graphics g)
              {
               for(int i=0; i<=msg.length(); i++)
                   g.drawString(msg.charAt(i),state+i,50) //state+i 依次绘出单个字符
                                                          //行固定不变     
              }
            由于我在网吧上网没有调试环境 所以这个程序我并没有测试 其中可能存在一起错误,不过基本是这个思路 我会继续关注 有不清楚的给我回复