135-1821-9792

java文字移动代码 java代码设置字体

Java编程实现一个能循环地自左向右移动的动画程序。

效果图

成都创新互联专注于双塔企业网站建设,响应式网站建设,商城建设。双塔网站建设公司,为双塔等地区提供建站服务。全流程按需规划网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MoveTextFrame extends JFrame {

JLabel jl;//文字标签

int speed=2;//移动速度

public MoveTextFrame() {

jl = new JLabel("文字动画");

jl.setForeground(Color.RED);

add(jl);

setSize(380, 100);//窗口大小

setLocationRelativeTo(null);//窗口居中

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

//设置定时器, 每隔25毫秒,改变一次文字标签的位置

Timer t = new Timer(25, new ActionListener() {

public void actionPerformed(ActionEvent e) {

int x = jl.getX()+speed;//计算移动后的位置

if(x=390){//如果超过就指定像素,就重新从左边开水移动

x=-30;

}

jl.setLocation(x, jl.getY());//更新位置

//repaint();

}

});

t.start();

}

public static void main(String[] args) {

new MoveTextFrame();

}

}

Java程序,实现字幕滚动,代码如下,帮我查查错误在哪?程序可以运行汉字就是不动

加入

public void init()

{

new Thread(this).start();

}

这个是Applet生命周期中的初始化调用,这里启用线程即可。

删除public static void main(String[] args) {

new Thread(new RollWords()).start();

},这个没用,Applet不用main执行,而是用appletViewer或者浏览器执行。

用Java编写一个字符串循环滚动的程序

java里面有一个叫做Timer的东西

代码找到了:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

////////////////////////////////////////////////////////////

public class AnimationDemo extends JFrame{

AnimationDemo(){

add(new MPanel("我是要移动的文字"));

}

////////////////////////////////////////////////////////////

public static void main(String[] args){

JFrame frame=new AnimationDemo();

frame.setTitle("AnimationDemo");

frame.setSize(280, 100);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

/////////////////////////////////////////////////////////////

static class MPanel extends JPanel{

private String message="welcome to java!";

private int xZuoBiao=0;

private int yZuoBiao=30;

//...........................................................

public MPanel(String message){

this.message=message;

Timer timer=new Timer(100,new TimerListener());

timer.start();

}

//............................................................

protected void paintComponent(Graphics g){

super.paintComponent(g);

if(xZuoBiaogetWidth()){

xZuoBiao=-20;

}

xZuoBiao+=10;

g.drawString(message, xZuoBiao, yZuoBiao);

}

//.............................................................

class TimerListener implements ActionListener{

public void actionPerformed(ActionEvent e){

repaint();

}

}

}

}


标题名称:java文字移动代码 java代码设置字体
本文URL:http://kswsj.com/article/dopsijp.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号