如何使用Java博士中的鼠标侦听器识别鼠标输入的按钮

时间:2015-01-12 16:51:54

标签: java user-interface mouseevent mouse-listeners

如何根据鼠标悬停在哪个按钮上将Jlabel info3设置为不同的文本消息?现在我不知道如何区分鼠标当前悬停在哪个按钮上。这是我的代码

import javax.swing.*;//You need these two libraries to do the GUI stuff
import java.awt.*;
import java.awt.event.*;

public class GUI_integrator extends JFrame implements ActionListener, MouseListener//You are telling Java that this is a GUI application
{ 

  // *** CREATE THE GUI COMPONENTS: Here we are only creating the following: Buttons, Labels, and Text field

  //buttons
  JButton EI = new JButton ("Emotional intelligence");//emotional intelligence test button
  JButton MI = new JButton ("Multiple intelligences");//multiple intelligences test
  JButton MB = new JButton ("Myers-Briggs assessment");//myers-briggs assessment
  JButton LS = new JButton ("Learning styles");//learning styles test
  JButton CP = new JButton ("Career preferences");//career preferences test

  //panels
  JPanel p1 = new JPanel();// panel for the information
  JPanel p2 = new JPanel();//panel for the selection of tests
  JPanel p3 = new JPanel();// panel to display information on the tests

  //labels
  JLabel info1 = new JLabel("Welcome! Please take a variety of tests so information can be gathered in order to determine what ",JLabel.RIGHT);
  JLabel info2 = new JLabel ("learning strategies will be most beneficial to you ", JLabel.RIGHT);
JLabel info3 = new JLabel ("Select the desired test");

// **********                      

  // *** CONSTRUCTOR - Has the same name as the class and it runs once (when the program begins)
  // This is were the GUI should be configured.

  public GUI_integrator() //This section adds the components on to the frame
  { 
    setTitle("Psychology Simulator 2015");  //Set the window title or  frame
    setSize(640, 480);         //Set the dimensions of

    FlowLayout fl1 = new FlowLayout(); //used to organize window

    GridLayout g1 = new GridLayout();
    g1.setColumns(1);
    g1.setRows(5);
    setLayout(g1);

//set the layout of the panels      
    p1.setLayout(fl1);
    p2.setLayout(fl1);
p3.setLayout(fl1); 

//add welcome greeting to first panel
    p1.add(info1);
    p1.add(info2);
p3.add(info3);    

//add buttons to second panel
    p2.add(EI);
    p2.add(MI);
    p2.add(MB);
    p2.add(LS);
    p2.add(CP);

    //add the panels
    add(p1);
    add(p2);
    add(p3);

//add action listeners to buttons
    EI.addActionListener(this);
    MI.addActionListener(this);
    MB.addActionListener(this);
    LS.addActionListener(this);
    CP.addActionListener(this);

    //add mouse listeners
    EI.addMouseListener(this);
    MI.addMouseListener(this);
    MB.addMouseListener(this);
    LS.addMouseListener(this);
    CP.addMouseListener(this);


    setVisible(true);       // display the gui on the screen
    setResizable(false); //the user cannot resize the window
  }

  public void actionPerformed(ActionEvent event)
  {
    String command =event.getActionCommand();

//check to see if buttons are pressed
    if (command.equals ("Emotional intelligence"))
    {}
    if (command.equals("Multiple intelligences"))
    {}
    if (command.equals("Myers-Briggs assessment"))
    {}
    if (command.equals("Learning styles"))
    {}
    if (command.equals("Career preferences"))
    {}


  }

public void mouseExited(MouseEvent event)
{}

public void mousePressed(MouseEvent event)
{}

public void mouseEntered(MouseEvent event)
{info3.setText("hi");}

public void mouseReleased (MouseEvent event)
{}

public void mouseClicked(MouseEvent event)
{}

// **** This is the main method - It just starts the GUI
  public static void main(String[] args) {
    GUI_integrator frame1 = new GUI_integrator();  //start the GUI!
  }

}

1 个答案:

答案 0 :(得分:1)

有关生成事件的Component的信息包含在MouseEvent本身中:

public void mouseEntered(MouseEvent event)
{
    Component c = event.getComponent();
    System.out.println(c);
}

对于其他活动,您可以使用getSource()方法。