将鼠标放在按钮上时如何移动按钮

时间:2017-07-28 08:38:15

标签: java swing jframe

我有一个带有两个按钮的Jframe:“是”和“否”。每当用户将鼠标悬停在按钮上时,我想移动“是”按钮,这样用户将始终“追逐”它。

以下是代码:

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
// import javax.swing.JLabel;
// import javax.swing.JPanel;

public class BeachPartyPrimerFrame extends JFrame 
{
   private JPanel mousePanel; // panel in which mouse events will occur
   private JLabel statusBar; // label that displays event information
   private JLabel Party, study;
   private JButton YesButton, Yes2Button, NoButton;
   private int locx []= {200, 100, 500, 200};
   private int locy [] = {20, 200, 200, 300};
   private int count = 0;
   private int mod = 4;


   private boolean nobuttonhit = false;
   // MouseTrackerFrame constructor sets up GUI and 
   // registers mouse event handlers
   public BeachPartyPrimerFrame()
   {
      super( "Demonstrating Mouse Events" );

      mousePanel = new JPanel(); // create panel
      mousePanel.setBackground( Color.WHITE ); // set background color
      mousePanel.setLayout(null);
      add( mousePanel, BorderLayout.CENTER ); // add panel to JFrame
      statusBar = new JLabel( "Mouse outside JPanel" ); 
      add( statusBar, BorderLayout.SOUTH ); // add label to JFrame
      Party = new JLabel ("Do you want Beach party right now?");
      add (Party, BorderLayout.NORTH);
      study = new JLabel ("Great! Keep studying hard!!");
      study.setVisible(false);
      study.setFont(new Font("Serif",Font.PLAIN, 24));
      study.setForeground(Color.RED);
      study.setSize(400,80);
      study.setLocation(100,200);
      YesButton = new JButton ("Yes");
      YesButton.setSize (60,40);
      YesButton.setLocation(200, 20);


      NoButton = new JButton ("No");
      NoButton.setSize(60,40);
      NoButton.setLocation (320, 20);
      ButtonHandler bhandler = new ButtonHandler();
      YesButton.addActionListener (bhandler);
      NoButton.addActionListener(bhandler);

      mousePanel.add(YesButton);
      mousePanel.add(NoButton);
      mousePanel.add(study);

      // create and register listener for mouse and mouse motion events
      MouseHandler handler = new MouseHandler(); 
      mousePanel.addMouseListener( handler ); 
      mousePanel.addMouseMotionListener( handler );   


   } // end MouseTrackerFrame constructor
   public void hideButtons () {
       System.out.println ("Try to hide buttons");
       YesButton.setVisible(false);
   }
   private class ButtonHandler implements ActionListener 
   {
      // handle button event
      public void actionPerformed( ActionEvent event )
      {

         if (event.getActionCommand() == "No") {
             System.out.println ("No button hit");

         }

         else {
             System.out.println ("Yes button pressed");
             YesButton.setLocation(YesButton.getX(), 
                     YesButton.getY() + 60);
         }
      } // end method actionPerformed
   } // end private inner class ButtonHandler
   private class MouseHandler implements MouseListener, 
      MouseMotionListener 
   {
      // MouseListener event handlers
      // handle event when mouse released immediately after press
      public void mouseClicked( MouseEvent event )
      {
         statusBar.setText( String.format( "Clicked at [%d, %d]", 
            event.getX(), event.getY() ) );
         if (event.isMetaDown())
             System.out.println("Right button clicked");
         else
             System.out.println("Left button clicked");

      } // end method mouseClicked

      // handle event when mouse pressed
      public void mousePressed( MouseEvent event )
      {
         statusBar.setText( String.format( "Pressed at [%d, %d]", 
            event.getX(), event.getY() ) );
      } // end method mousePressed

      // handle event when mouse released after dragging
      public void mouseReleased( MouseEvent event )
      {
         statusBar.setText( String.format( "Released at [%d, %d]", 
            event.getX(), event.getY() ) );
      } // end method mouseReleased

      // handle event when mouse enters area
      public void mouseEntered( MouseEvent event )
      {
         statusBar.setText( String.format( "Mouse entered at [%d, %d]", 
            event.getX(), event.getY() ) );
         mousePanel.setBackground( new Color (255, 255,0));
      } // end method mouseEntered

      // handle event when mouse exits area
      public void mouseExited( MouseEvent event )
      {
         statusBar.setText( "Mouse outside JPanel" );
         mousePanel.setBackground( Color.WHITE );
      } // end method mouseExited

      // MouseMotionListener event handlers
      // handle event when user drags mouse with button pressed
      public void mouseDragged( MouseEvent event )
      {
         statusBar.setText( String.format( "Dragged at [%d, %d]", 
            event.getX(), event.getY() ) );
      } // end method mouseDragged

      // handle event when user moves mouse
      public void mouseMoved( MouseEvent event )
      {


         statusBar.setText( String.format( "Moved at [%d, %d]", 
            event.getX(), event.getY() ) );

      } // end method mouseMoved
   } // end inner class MouseHandler
} // end class MouseTrackerFrame

这是司机班:

import javax.swing.JFrame;
public class BeachPartyPrimer 
{
   public static void main( String args[] )
   { 
      BeachPartyPrimerFrame mouseTrackerFrame = new BeachPartyPrimerFrame(); 
      mouseTrackerFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      mouseTrackerFrame.setSize( 600, 400 ); // set frame size
      mouseTrackerFrame.setVisible( true ); // display frame
   } // end main
} // end class MouseTracker

以下是它的外观截图: Screenshot of program

目前,如果我点击“是”,按钮会向下移动一点: enter image description here

但是正如我所说的那样,我想改变它并使其成为用户总是在追逐它。

1 个答案:

答案 0 :(得分:1)

您只需将MouseListener添加到YesButton即可。覆盖mouseEntered方法并为按钮设置随机位置:

  YesButton.addMouseListener(new MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            YesButton.setLocation(new Random().nextInt(600), new Random().nextInt(400));
        }
    });

我在范围xy(您的窗口大小)范围内为600400创建了一个随机位置,但您当然可以调整它。