问题描述:在一个类似幕布的显示牌上从右向左滚动字符(如果你的语言使用这种习惯,也可以从左到右)。可以选择用一个循环显示文本,这样当文本移出显示牌的一端后,它会重新从另一端滚出。小弟出学java。请大家看一下,怎么做?谢谢!

解决方案 »

  1.   

    http://dev.csdn.net/article/73/73551.shtm
      

  2.   

    我是个超级菜鸟。没有写过程序。
    你可以把JLabel的位置放到最右边。调用JLable的setLocation(int x,int y)然后把x值不断减少。可以把它写到一个方法中,然后用一个独立的线程来调用这个方法。就可以实现滚动延迟了.我把这个类实现了Runnable接口。
    例如
    import java.awt.BorderLayout;
    import java.awt.Point;import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class ScrollText extends JFrame implements Runnable{    private JPanel jContentPane = null;    private JDesktopPane mainDesktopPane = null;    private JLabel scrollLabel = null;
        
        private Thread thread=null;
           /**
         * This method initializes jDesktopPane
         * 
         * @return javax.swing.JDesktopPane
         */
        private JDesktopPane getMainDesktopPane() {
            if (mainDesktopPane == null) {
                scrollLabel = new JLabel();
                scrollLabel.setBounds(new java.awt.Rectangle(500,10,40,20));
                scrollLabel.setText("我爱婷");
                mainDesktopPane = new JDesktopPane();
                mainDesktopPane.add(scrollLabel, null);
            }
            return mainDesktopPane;
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub        ScrollText application = new ScrollText();
            application.thread.start();
           
            application.setVisible(true);
           
        }    /**
         * This is the default constructor
         */
        public ScrollText() {
            super();
            initialize();
        }    /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
                 
            
            scrollLabel=new JLabel("我爱你");
            this.setContentPane(getJContentPane());
            thread=new Thread(this);
           
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(300, 298);
            this.setTitle("Application");
        }    /**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
        private JPanel getJContentPane() {
            if (jContentPane == null) {
                jContentPane = new JPanel();
                jContentPane.setLayout(new BorderLayout());
                jContentPane.add(getMainDesktopPane(), java.awt.BorderLayout.CENTER);
            }
            return jContentPane;
        }    private void startScroll()
        {
          
            while (thread!=null) {
                
                Point p=scrollLabel.getLocation() ;
                p.x=(p.x-1)%this.getWidth();
                if(p.x==0) p.x=this.getWidth();
                scrollLabel.setLocation(p);
                try {
                    thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }        }
        }    public void run() {
            // TODO Auto-generated method stub
            startScroll();
         
        } 
    }  //  @jve:decl-index=0:visual-constraint="163,-19"
      

  3.   

    用到多线程,结合timer来设置