java eclips 字的滚动怎么做
1个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class B01 extends JFrame implements ActionListener, Runnable {
private JButton btn = new JButton("暂停");
private JLabel lbl = new JLabel("暂时是一段字");
private int step = 2;
private boolean running = true;
public B01() {
this.setLayout(null);
btn.setBounds(100, 120, 100, 40);
lbl.setBounds(210, 80, 80, 20);
btn.addActionListener(this);
add(lbl);
add(btn);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
start();
}
public void actionPerformed(ActionEvent e) {
if (running) {
running = false;
btn.setText("启动");
} else {
running = true;
btn.setText("暂停");
start();
}
}
private void start() {
new Thread(this).start();
}
public void run() {
while (running) {
int x = lbl.getLocation().x;
if (x > step) {
x -= step;
} else {
x = 210;
}
lbl.setBounds(x, 80, 80, 20);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
public static void main(String[] args) {
new B01();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class B01 extends JFrame implements ActionListener, Runnable {
private JButton btn = new JButton("暂停");
private JLabel lbl = new JLabel("暂时是一段字");
private int step = 2;
private boolean running = true;
public B01() {
this.setLayout(null);
btn.setBounds(100, 120, 100, 40);
lbl.setBounds(210, 80, 80, 20);
btn.addActionListener(this);
add(lbl);
add(btn);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
start();
}
public void actionPerformed(ActionEvent e) {
if (running) {
running = false;
btn.setText("启动");
} else {
running = true;
btn.setText("暂停");
start();
}
}
private void start() {
new Thread(this).start();
}
public void run() {
while (running) {
int x = lbl.getLocation().x;
if (x > step) {
x -= step;
} else {
x = 210;
}
lbl.setBounds(x, 80, 80, 20);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
public static void main(String[] args) {
new B01();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询