在我的课程中,我想向主要的JPanel添加其他JPanel obj在子类中创建的dinamically并捕获鼠标事件,确切地知道女巫obj生成它。 我尝试通过创建子类DoSquare(JPanel扩展),添加侦听器,创建objs并绘制该子类的Graphics来做到这一点,但是当我运行程序并单击创建的一个DoSquare obj时,e.getSource()总是返回SubObj对象而不是单击的元素。 如何区分Listener事件中选择的组件?
这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SubObj extends JPanel {
Listener listener = new Listener();
DoSquare firstQ = new DoSquare(100, 50);
DoSquare secondQ = new DoSquare(200, 70);
public static void main(String[] args) {
JFrame panel = new JFrame();
SubObj content = new SubObj();
panel.setContentPane(content);
panel.setFocusable(true);
panel.setSize(400, 400);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
SubObj() {
addMouseListener(listener);
addFocusListener(listener);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
firstQ.draw(g, Color.BLACK);
secondQ.draw(g, Color.CYAN);
}
private class DoSquare extends JPanel {
private int coordX;
private int coordY;
Listener squareLis = new Listener();
DoSquare(int X, int Y) {
addMouseListener(squareLis);
addFocusListener(squareLis);
coordX = X;
coordY = Y;
}
void draw(Graphics g, Color c) { // Draw the square
g.setColor(c);
g.fillRect(coordX, coordY, 50, 50);
}
}
public class Listener implements MouseListener, FocusListener {
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
System.out.println(e.getSource());
}
public void mouseReleased(MouseEvent e) {}
public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {}
}
}
提前致谢
AM
答案 0 :(得分:2)
在SubObj
班级的构造函数中,您应该执行以下操作:
SubObj() {
firstQ.addMouseListener(listener);
secondQ.addFocusListener(listener);
}
并删除squareLis
课程中的DoSquare
变量。
<强>更新强>
public class SubObj extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
SubObj content = new SubObj();
frame.setContentPane(content);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
SubObj() {
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getSource());
System.out.println("click from " + ((TestPanel) e.getSource()).getId());
}
};
JPanel testPanel1 = new TestPanel("1");
testPanel1.setPreferredSize(new Dimension(200, 200));
testPanel1.setBackground(Color.BLACK);
testPanel1.addMouseListener(mouseAdapter);
add(testPanel1);
JPanel testPanel2 = new TestPanel("2");
testPanel2.setPreferredSize(new Dimension(100, 100));
testPanel2.setBackground(Color.RED);
testPanel2.addMouseListener(mouseAdapter);
add(testPanel2);
}
private class TestPanel extends JPanel {
private String id;
TestPanel(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
}
我上面写的代码等同于你想要实现的代码,它以你想要的方式工作。我不会使用您的代码,因为我不确定您希望它是什么。我的代码用于演示两个子面板在点击时如何响应鼠标事件,这是我们关注此问题的重点。