58,454
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class Test4 extends JApplet{
private JLabel label=new JLabel("欢迎来到Java的世界!");
private Timer timer;
public void init(){
setLayout(null);
add(label);
label.setBounds(300, 60, 150, 40);
timer=new Timer(100,new ActionListener(){
public void actionPerformed(ActionEvent e){
label.setLocation(label.getLocation().x-5, label.getLocation().y);
if(label.getLocation().x+150<=0){
label.setBounds(300, 60, 150, 40);
timer.restart();
}
}
});
timer.setInitialDelay(0);
}
public void setlabelsize(int width,int height){
label.setBounds(width, height/2, 150, 40);
}
public void start(){
timer.start();
}
public void stop(){
timer.stop();
}
public static void main(String[] arge){
JFrame frame=new JFrame("Test4");
Test4 applet=new Test4();
frame.add(applet);
applet.start();
applet.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,120);
frame.setVisible(true);
}
}