开发者

Handling Click for A Custom Swing component Extending JPanel?

开发者 https://www.devze.com 2023-02-21 14:44 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号