展开全部
描述得有点不清楚,看看这个是不是你想要的
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class RunningButton extends JFrame implements ActionListener {
private JButton btn1, btn2;
private int dir1 = -1, dir2 = -1; // -1是向左,1是向右
private static int SPEED = 10; // 固定的速度
private int speed1 = 0, speed2 = 0; // 可变的速度
public RunningButton() {
super("按钮快跑");
}
private void launch() {
setVisible(true);
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
btn1 = new JButton("按钮1");
getContentPane().add(btn1);
btn2 = new JButton("按钮2");
getContentPane().add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setLocationRelativeTo(null);
new Thread(new ButtonThread()).start();
}
public static void main(String[] args) {
new RunningButton().launch();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == btn1 && 0 == speed1) {
speed1 = SPEED;
} else if(source == btn2 && 0 == speed2) {
speed2 = SPEED;
}
}
class ButtonThread implements Runnable {
@Override
public void run() {
while(true) {
try {
Thread.sleep(30);
btn1Run();
btn2Run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void btn1Run() {
if(0 == speed1) {
btn1.setLocation(btn1.getX(), 300);
return;
}
int speed = dir1 * speed1;
btn1.setLocation(btn1.getX() + speed, 300);
if(btn1.getX() <= 0) {
speed1 = 0;
dir1 *= -1;
btn1.setLocation(0, 300);
} else if(btn1.getX() >= btn2.getX() - btn1.getWidth()) {
speed1 = 0;
dir1 *= -1;
btn1.setLocation(btn2.getX() - btn1.getWidth(), 300);
}
}
private void btn2Run() {
if(0 == speed2) {
btn2.setLocation(btn2.getX(), 300);
return;
}
int speed = dir2 * speed2;
btn2.setLocation(btn2.getX() + speed, 300);
if(btn2.getX() >= getWidth() - btn2.getWidth()) {
speed2 = 0;
dir2 *= -1;
btn2.setLocation(getWidth() - btn2.getWidth(), 300);
} else if(btn2.getX() <= btn1.getX() + btn1.getWidth()) {
speed2 = 0;
dir2 *= -1;
btn2.setLocation(btn1.getX() + btn1.getWidth(), 300);
}
}
}
这个就是点击了以后按钮会朝着一个方向跑,碰到边界或另一个按钮就停下来。然后当按钮停下来了以后再点击时如果没有障碍就朝反方向跑。
更多追问追答
追问
那怎么做能让光标快要碰上还没碰上,类似于推着按钮走,这个按钮可以从什么方向都可以移动靠近另一个按钮,另一个按钮也可以达到这个效果
追答
这个有点难度,还不如做成拖拽
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询