简体   繁体   English

如何使用鼠标侦听器调用另一个类?

[英]How do I use a mouse listener to call another class?

I have bits and and pieces of the mouse listener, but I am not sure how to tie it all together.我有鼠标监听器的零碎部分,但我不知道如何将它们联系在一起。 I am trying to have a tic tac toe game activate when the user clicks on the play games button.当用户点击玩游戏按钮时,我试图激活井字游戏。 Assistance would be greatly appreciated.将不胜感激。

    import java.awt.*;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.EventQueue;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import java.awt.GridBagLayout;
    import java.awt.event.*;
    import java.awt.event.ActionListener.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JOptionPane;
    import java.awt.Color;

    public class Use_PF_Interface extends JFrame implements Pet_Fish_Interface, MouseListener
     {
        // instance variables - replace the example below with your own
        private JFrame window;
        private JPanel backgroundPanel;
        private JLabel lblBackgroundImage = new JLabel();
        private JButton feedButton = new JButton("Feed Fish");
        private JButton playGamesButton = new JButton("Play Game");
        volatile private boolean mouseDown = false;

        //creates frame window
        public Use_PF_Interface()
        {
           setTitle("Virtual Pet Fish");
           setSize(400, 400);

           //initializes panels and panel layout
           backgroundPanel = new JPanel(); 
           backgroundPanel.setOpaque(false); 
           backgroundPanel.setLayout(new FlowLayout());

           lblBackgroundImage.setLayout(new FlowLayout());

           //sets background image of panel
           lblBackgroundImage.setIcon(new ImageIcon("OCEAN2.jpg"));
           lblBackgroundImage.setLayout(new BorderLayout()); 
           lblBackgroundImage.setBounds(0, 0, 400, 400);


            //adds button to panels
            backgroundPanel.add(feedButton);
            backgroundPanel.add(playGamesButton);
            lblBackgroundImage.add(backgroundPanel);


            add(lblBackgroundImage);


            feedButton.addMouseListener(this);
            playGamesButton.addMouseListener(listener);

            addMouseListener(this);


            }//creates frame window

         /**
         * This method will create an action for the button.
         * @pre none
         * @return tic tac toe game
         * @param none
         * @post none
         */

         public void mousePressed(MouseEvent e)
         {

             if (e.getButton() ==  MouseEvent.playGamesButton)
             {
               mouseDown = true;
               Tic_Tac_Toe();
              }
        }

        public void mouseReleased(MouseEvent e)
        {
             if(e.getButton() == MouseEvent.feedButton)
             {
                mouseDown = false;

             }
         }

          public void mouseExited(MouseEvent e)
          {
          }

I am currently recieving this error: Use_PF_interface is not abstract and does not override abstract method mouseEntered(java.awt.event.MouseEvent) in java.awt.event.MouseListener我目前收到此错误: Use_PF_interface 不是抽象的,并且不会覆盖 java.awt.event.MouseListener 中的抽象方法 mouseEntered(java.awt.event.MouseEvent)

The solution is very simple.解决方法很简单。 MouseListener is an Interface. MouseListener 是一个接口。 An Interface is a class with methods, which the class that implements this interface, must contain.接口是具有方法的类,实现此接口的类必须包含这些方法。 Even if these methods contain nothing, you have to include them in your code.即使这些方法不包含任何内容,您也必须将它们包含在您的代码中。 So if you want to implement the MouseListener you have to override these 5 methods:所以如果你想实现 MouseListener 你必须覆盖这 5 个方法:

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class Example implements MouseListener {

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}


}

And by the way, can't you just use an ActionListener for that, what you're trying to do?顺便说一句,您不能为此使用 ActionListener 吗,您正在尝试做什么?

不太确定这是否是问题所在,但是该类中是否应该有一个 mouseEntered 方法来覆盖如上所述?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM