/* * Demo.java * * Created on 2004年11月12日, 上午10:27 */package test.text;/** * * @author zhaobc */ public class Demo extends javax.swing.JFrame {
/** Creates new form Demo */ public Demo() { initComponents(); Thread1 thread = new Thread1("This is a text!"); thread.start(); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() { absoluteLayout1 = new org.netbeans.lib.awtextra.AbsoluteLayout(); jLabel1 = new javax.swing.JLabel(); getContentPane().setLayout(null); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jLabel1.setText("jLabel1"); getContentPane().add(jLabel1); jLabel1.setBounds(30, 80, 350, 30); pack(); }
<<
public class Program {
public static void main(String[] args) {
System.out.println("一行字 在窗体中循环滚动!(从右到左)");
}
}
>>
“一行字 在窗体中循环滚动!(从右到左)”
</marquee>
一行字.滚动();
笑死人了不过这个程序在java里面实现好像不算简单的
在一个循环里给这个方法传参数,就实现了让字符串的locate从右往左动了我觉得必须要用线程,每次改变位置要sleep(500),不然就看不出效果了,呵呵
* Demo.java
*
* Created on 2004年11月12日, 上午10:27
*/package test.text;/**
*
* @author zhaobc
*/
public class Demo extends javax.swing.JFrame {
/** Creates new form Demo */
public Demo() {
initComponents();
Thread1 thread = new Thread1("This is a text!");
thread.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
absoluteLayout1 = new org.netbeans.lib.awtextra.AbsoluteLayout();
jLabel1 = new javax.swing.JLabel(); getContentPane().setLayout(null); addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
}); jLabel1.setText("jLabel1");
getContentPane().add(jLabel1);
jLabel1.setBounds(30, 80, 350, 30); pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Demo().show();
}
// Variables declaration - do not modify
private org.netbeans.lib.awtextra.AbsoluteLayout absoluteLayout1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
class Thread1 extends Thread
{
private String text = "";
public Thread1(String _text)
{
this.text = _text;
}
public void run()
{
int maxl = jLabel1.getWidth()/10;
char[] c = text.toCharArray();
int[] cp = new int[c.length];
for(int i=0;i<cp.length;i++)
cp[i] = i;
for(int i=0;i<1000;i++)
{
int ps = i%maxl;
String temp = "";
for(int j=0;j<maxl;j++)
{
boolean f = false;
for(int k=0;k<c.length;k++)
{
int cps = (ps+k)%maxl;
if(j==cps)
{
temp += c[k];
f = true;
break;
}
}
if(!f) temp += " ";
}
jLabel1.setText(temp);
try{Thread.sleep(200);}catch(Exception e){}
}
}
}
}
int[] cp = new int[c.length];
for(int i=0;i<cp.length;i++)
cp[i] = i;
import java.applet.*;public class TextTest extends Applet implements Runnable{
String msg = "123456789";
Thread t = null;
int state;
boolean stopflag=false;
public void init() {
} public void paint(Graphics g) {
g.drawString(msg, 50, 60 );
}
public void start()
{
t= new Thread(this);
t.start();
}
public void run()
{
char ch;
for(;;)
{
try{
repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1,msg.length());
msg+=ch;
if(stopflag)
break;
}
catch(InterruptedException e)
{}
}
}
public void stop()
{
stopflag = true;
t=null;
}
}