📄 scolling.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Scolling extends Applet implements Runnable,ActionListener{
//用于设定字符在小应用程序中的位置
int xPoint,yPoint;
//string用于接收文本框text中的字符
String string;
TextField text;
//线程的启动器
Button okBtn;
//定义一个用来滚动字幕的线程
Thread words=null;
public void init()
{
//成员变量的初期化
xPoint=0;
yPoint=50;
string="";
okBtn=new Button("OK");
text=new TextField();
//设置小应用程序的背景、文字的颜色、还有文字的类型与大小
setBackground(Color.LIGHT_GRAY);
setForeground(Color.RED);
setFont(new Font("TimesRoman",Font.BOLD,20));
//设定布局为空布局
setLayout(null);
//向程序中添加组件,并设置位置及大小
add(text);
add(okBtn);
text.setBounds(0,0,330,50);
okBtn.setBounds(330,0,70,50);
//向按钮添加监视器
okBtn.addActionListener(this);
}
public void start() {
//通常线程都是在start中启动,但并不是一定要在这里启动
}
public void run () {
//如果线程存在,则执行
while(words !=null) {
//使x与y变化,已达到字幕滚动的效果,注意防止字幕滚出边界
if(xPoint<200){
xPoint+=5;
if(yPoint<400){
yPoint+=5;
}else{
yPoint=100;
}
}else{
xPoint=0;
}
repaint();
try{
//使线程休眠300毫秒
words.sleep(300);
} catch(InterruptedException e){
}
}
}
public void paint(Graphics g){
//输出字幕
g.drawString(string,xPoint,yPoint);
}
public void stop(){
//释放线程
words.yield();
words=null;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==okBtn){
//从文本框中取字符
string=text.getText();
if(!string.equals("")){
//不为空则启动线程
if (words ==null){
words =new Thread(this);
words.start();
}
} else {
//否则输出下列字符
xPoint=20;
yPoint=200;
string="我什么都没有写,所以线程不会启动!";
repaint();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -