我有点鼠标监听器,但我不知道如何将它们绑在一起。当用户点击播放游戏按钮时,我试图激活一个tic tac toe游戏。非常感谢协助。
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)
{
}
我目前收到此错误:Use_PF_interface不是抽象的,并且不会覆盖java.awt.event.MouseListener中的抽象方法mouseEntered(java.awt.event.MouseEvent)
答案 0 :(得分:1)
解决方案非常简单。 MouseListener是一个接口。接口是一个带有方法的类,实现此接口的类必须包含这些方法。即使这些方法不包含任何内容,您也必须将它们包含在代码中。因此,如果要实现MouseListener,则必须覆盖以下5种方法:
new DataAccessException("...") {}
顺便说一下,你不能只使用一个ActionListener,你正在尝试做什么?
答案 1 :(得分:0)
不太确定这是否是问题,但该类中是否应该有一个mouseEntered方法来覆盖?