How would you handle click in a Custom swing Component that is extending JPanel. I am making a game and I am wondering how i can handle the clicks. Specifically in a different thread than the gui, and painting? How would I do this here is my code:
package com.games.bubblecards;
开发者_StackOverflow中文版 import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Canvas extends JPanel {
private static final long serialVersionUID = 1L;
public Canvas() {
super();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawString("Hello It Works", 10, 10);
}
}
You handle mouse clicks by adding a MouseListener to your component, and this is no different for standard Swing components or custom components that extend Swing components. The mouse listener's methods must be called on the main Swing thread, the EDT (and by default this is what happens without need for special code), however the listener code can then set up and start background threads if desired.
精彩评论