如何将输入从MouseListener发送到另一个类?

时间:2014-05-21 02:01:49

标签: java class mouse

我有一个运行启动画面的跑步者类,然后运行主菜单。从主菜单中,根据用户选择的选项,应显示不同的屏幕。

问题是我使用MouseListener来检测用户点击了哪个选项。然后需要将此值传递给runner类以确定接下来要显示的类。

我不能用一种有效的方法来解决这个问题。目前,我已经掌握了它在执行开始时获取鼠标位置的地方,并且再也没有找到它。我也尝试了一个while循环[虽然鼠标位置不在按钮顶部再次获得鼠标位置]但显然我们不想使用while循环,因为这是低效的。

这个网站上的其他几个线程建议让我的跑步者类实现接口然后参数 - 将它传递到主菜单...这对我来说没有多大意义,因为我不应该跑步class不是子类或实现任何接口?

这是我的代码:

主菜单类

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;

public class MainMenu extends JPanel implements ActionListener, MouseListener{ //ActionListener?
  Image image;
  int loc = 0;

  public MainMenu(){
    initializeImage();
    addMouse();
  }

  public void initializeImage(){
    try{
      image = ImageIO.read(new File("MainMenu.jpg"));
      //JLabel picLabel = new JLabel(new ImageIcon(image));
      //add(picLabel);
    }
    catch(IOException e){}
  }

  public int getLoc()
  {
   return loc; 
  }

  public void addMouse(){
    addMouseListener
      (new MouseAdapter()
                       {
      public void mouseReleased(MouseEvent e)
      {
        loc = e.getY();
      }                
    }
    );
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent e) { //Add proposed helper method!
        if (e.getX() >= 200 && e.getX() <= 800){
          try{
            if (e.getY() >= 241 && e.getY() <= 309){
              image = ImageIO.read(new File("MainMenu1.jpg"));  
              repaint();
            }
            else if (e.getY() >= 349 && e.getY() <= 412){
              image = ImageIO.read(new File("MainMenu2.jpg"));  
              repaint();
            }
            else if (e.getY() >= 451 && e.getY() <= 516){
              image = ImageIO.read(new File("MainMenu3.jpg"));  
              repaint();
            }
            else if (e.getY() >= 552 && e.getY() <= 619){
              image = ImageIO.read(new File("MainMenu4.jpg"));  
              repaint();
            }
            else{
              image = ImageIO.read(new File("MainMenu.jpg"));  
              repaint();
            }
          }
          catch(IOException ee){}

        }
      }
    });
  }

  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters            
  }

  public void actionPerformed(ActionEvent e){}

  public void mouseClicked(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}

亚军类

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/** 
  * @author Horatiu Lazu
  * Incomplete class.*/

public class ClassRunner extends JFrame{

  public ClassRunner(){
    setSize(960, 661);
    LevelSelection l = new LevelSelection();
    add(l);
    setVisible(true); 
  }

  public static void main (String [] args){
    new ClassRunner();
  }
}

0 个答案:

没有答案