紧急!!!求解按钮随着鼠标的移动问题
我是java初学者,我想做一个小游戏,用鼠标控制按钮水平移动,接上面随机移动的小球。按鼠标事件,记住鼠标的坐标(x0,y0).鼠标拖动是,产生的坐标记作(x1,y1) 然后我给button的横坐标bx=bx+(x1-x0);x0=x1;然后这样下来,鼠标慢慢移动,按钮还能正常的移动,鼠标稍微一快,按钮就跳跃性的移动,甚至就移不见了
请教一下各位高手!!那个button的横坐标应该怎么控制才正确呢?
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MoveButton { private static int xOnComponent, xOnScreen; private static JFrame jFrame = new JFrame("Test"); private static JButton jButton = new JButton("button"); public static void main(String[] args) { // 初始化按钮 jButton.setSize(100, 25); jButton.setLocation( (500 - jButton.getWidth()) / 2, (300 - jButton.getHeight()) / 2); // 初始化窗口 jFrame.setLayout(null); jFrame.setSize(500, 300); jFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - jFrame.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - jFrame.getHeight()) / 2); jFrame.setVisible(true); jFrame.add(jButton); // 为按钮添加MouseListener jButton.addMouseListener(new MouseListener() { public void mousePressed(MouseEvent e) { xOnComponent = jButton.getX(); xOnScreen = e.getXOnScreen(); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }); // 为按钮添加MouseMotionListener jButton.addMouseMotionListener(new MouseMotionListener() { public void mouseDragged(MouseEvent e) { int xTmp1 = xOnComponent; int xTmp2 = e.getXOnScreen(); xTmp1 += xTmp2 - xOnScreen; if(xTmp1 > -1 && xTmp1 < 500 - jButton.getWidth()) { xOnComponent = xTmp1; xOnScreen = xTmp2; jButton.setLocation(xTmp1, jButton.getY()); } } public void mouseMoved(MouseEvent e) {} }); } }因为只需要水平移动,所以不需要考虑y。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MoveButton { private static int xOnFrame, xOnScreen; private static JFrame jFrame = new JFrame("Test"); private static JButton jButton = new JButton("button"); public static void main(String[] args) { // 初始化按钮 jButton.setSize(100, 25); jButton.setLocation( (500 - jButton.getWidth()) / 2, (300 - jButton.getHeight()) / 2); // 初始化窗口 jFrame.setLayout(null); jFrame.setSize(500, 300); jFrame.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - jFrame.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - jFrame.getHeight()) / 2); jFrame.setVisible(true); jFrame.add(jButton); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); // 为按钮添加MouseListener jButton.addMouseListener(new MouseListener() { public void mousePressed(MouseEvent e) { xOnFrame = jButton.getX(); xOnScreen = e.getXOnScreen(); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }); // 为按钮添加MouseMotionListener jButton.addMouseMotionListener(new MouseMotionListener() { public void mouseDragged(MouseEvent e) { int x1 = xOnFrame; int x2 = e.getXOnScreen(); x1 += x2 - xOnScreen; if(x1 < 0) { xOnFrame = 0; xOnScreen = max( min(jFrame.getX() + jButton.getWidth() - 1, xOnScreen), jFrame.getX()); } else if(x1 >= jFrame.getWidth() - jButton.getWidth()) { xOnFrame = jFrame.getWidth() - jButton.getWidth() - 1; xOnScreen = min( max(jButton.getX() + jFrame.getX() + 1, xOnScreen), jButton.getX() + jFrame.getX() + jButton.getWidth() - 1); } else { xOnFrame = x1; xOnScreen = x2; } jButton.setLocation(xOnFrame, jButton.getY()); } public void mouseMoved(MouseEvent e) {} }); } public static int max(int a, int b) { return a > b ? a : b; } public static int min(int a, int b) { return a < b ? a : b; } }