Java 用于 JPanel 的 addMouseListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16431455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addMouseListener for a JPanel
提问by user2360545
Today I have a problem..
My program make a 8x8 grid and show the coord when I click on a JButton
.
今天我遇到了一个问题..我的程序制作了一个 8x8 的网格并在我点击JButton
.
BUT I refuse to use JButton
and I need to go for JPanel
.. But my addMouseListener
isn't working so I don't know how is it possible to fix that I'm searching since 4h.....
但是我拒绝使用JButton
,我需要去JPanel
......但我addMouseListener
没有工作所以我不知道如何解决我从 4 小时开始搜索的问题.....
package coordboutons;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CoordBoutons extends JFrame {
CoordBoutons() {
super("GridLayout");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contenant = getContentPane();
contenant.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
contenant.add(new CaseEchiquier(i, j));
pack();
setVisible(true);
}
**class CaseEchiquier extends JPanel** {
private int lin, col;
CaseEchiquier(int i, int j) {
lin = i;
col = j;
setPreferredSize(new Dimension(80, 75));
setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println((char)('a' + col) + "" + (8 - lin));
}
});
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
CoordBoutons coordBoutons = new CoordBoutons();
}
}
采纳答案by MadProgrammer
JPanel
doesn't have ActionListener
capabilities. Instead, you need to use a MouseListener
JPanel
没有ActionListener
能力。相反,您需要使用MouseListener
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CoordBoutons extends JFrame {
CoordBoutons() {
super("GridLayout");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contenant = getContentPane();
contenant.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
contenant.add(new CaseEchiquier(i, j));
}
}
pack();
setVisible(true);
}
class CaseEchiquier extends JPanel {
private int lin, col;
CaseEchiquier(int i, int j) {
lin = i;
col = j;
setPreferredSize(new Dimension(80, 75));
setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
addMouseListener(new MouseAdapter() {
private Color background;
@Override
public void mousePressed(MouseEvent e) {
background = getBackground();
setBackground(Color.RED);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
setBackground(background);
}
});
// addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent evt) {
// System.out.println((char) ('a' + col) + "" + (8 - lin));
//
// }
// });
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
CoordBoutons coordBoutons = new CoordBoutons();
}
});
}
}
Take a look at How to Write Mouse Listenersfor more details...
查看如何编写鼠标侦听器以获取更多详细信息...
回答by Marlon Bernardes
The problem is that the method addActionListener
does not exists for a JPanel. You should use the appropriate listener for this case (java.awt.event.MouseListener
). Since MouseListener
is an interface (and you don't want to implement all of its methods), you could use a MouseAdapter
and override only the method(s) you need, like this:
问题是addActionListener
JPanel 不存在该方法。对于这种情况,您应该使用适当的侦听器 ( java.awt.event.MouseListener
)。由于MouseListener
是一个接口(并且您不想实现其所有方法),您可以使用 aMouseAdapter
并仅覆盖您需要的方法,如下所示:
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println((char)('a' + col) + "" + (8 - lin));
}
});