栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法在JFrame上绘制

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法在JFrame上绘制

  • 不要直接在上执行自定义绘画

    Jframe
    。如果可以,请始终在
    JComponent
    替代
    paintComponent
    方法上进行操作。

  • 为此,请勿使用无限循环。有

    MouseMotionListener
    用于Mouse Motion收听的


public class Test4 {    public static String a;    public static CustomDrawingPanel content;    public static Jframe frame = new Jframe();    final static int OVAL_WIDTH = 10;    final static int OVAL_HEIGHT = 10;    static int x = -20, y = -20;    public static MouseMotionListener listener = new ContentListener();    public static void main(String[] args) throws InterruptedException {        int h = 250;        int f = 200;        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        content = new CustomDrawingPanel();        content.addMouseMotionListener(listener);        frame.add(content);        frame.getContentPane().setPreferredSize(new Dimension(h, f));        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }    //class that performs custom drawing    static class CustomDrawingPanel extends JPanel {        public void paintComponent(Graphics g) { super.paintComponent(g);  //Always call this g.drawOval(x, y, 10, 10);        }    }    //listener to the mouse motion    static class ContentListener implements MouseMotionListener {        @Override        public void mouseDragged(MouseEvent e) { mouseMoved(e); //if you delete this line, when you drag your circle will hang        }        @Override        public void mouseMoved(MouseEvent e) { x = e.getX() - OVAL_WIDTH / 2; y = e.getY() - OVAL_HEIGHT / 2; content.repaint();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398311.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号