Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Dr Java中使用鼠标侦听器识别鼠标已输入的按钮_Java_User Interface_Mouseevent_Mouse Listeners - Fatal编程技术网

如何在Dr Java中使用鼠标侦听器识别鼠标已输入的按钮

如何在Dr Java中使用鼠标侦听器识别鼠标已输入的按钮,java,user-interface,mouseevent,mouse-listeners,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 ActionList

如何根据鼠标悬停在哪个按钮上,将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!
  }

}

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

public void mouseEntered(MouseEvent event)
{
    Component c = event.getComponent();
    System.out.println(c);
}
对于其他事件,可以使用
getSource()
方法