我正在尝试使用MouseListener来查看何时在我的JFrame中单击了某些东西,但是从未调用过任何事件(mouseClicked,mouseEntered等)。我正在使用Canvas将游戏绘制到JFrame上,我想知道是否可能是这个问题,但是我不确定如何修复它。我正在关注this教程,我的代码看起来几乎完全相同。
这是我的JFrame类:
public class Display extends JFrame implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JFrame frame;
private MyCanvas canvas;
public Display (int r, int c, Card[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
frame.setLayout(null);
frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new MyCanvas(b);
frame.add(canvas);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
您做错的主要事情是扩展JFrame并创建一个显式实例。请按照以下步骤操作。
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Display implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JPanel canvas;
private JFrame frame;
public static void main(String[] args) {
new Display(3,3,new int[3][3]);
}
public Display (int r, int c, int[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
// frame.setLayout(null);
// frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new JPanel();
canvas.setPreferredSize(new Dimension(width,height));
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
要考虑的要点
frame.setLocationRelativeTo(null)
]将框架在屏幕上居中。Card
类,所以我使用int[][]
对此进行了演示。有关GUI的更多信息,请查看Java Tutorials