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

如何绘制JPanel?(Swing / graphics Java)

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

如何绘制JPanel?(Swing / graphics Java)

注意额外的注释。

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;class JavaPaintUI extends Jframe {    private int tool = 1;    int currentX, currentY, oldX, oldY;    public JavaPaintUI() {        initComponents();    }    private void initComponents() {        // we want a custom Panel2, not a generic JPanel!        jPanel2 = new Panel2();        jPanel2.setBackground(new java.awt.Color(255, 255, 255));        jPanel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));        jPanel2.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) {     jPanel2MousePressed(evt); } public void mouseReleased(MouseEvent evt) {     jPanel2MouseReleased(evt); }        });        jPanel2.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) {     jPanel2MouseDragged(evt); }        });        // add the component to the frame to see it!        this.setContentPane(jPanel2);        // be nice to testers..        this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        pack();    }// </editor-fold>    private void jPanel2MouseDragged(MouseEvent evt) {        if (tool == 1) { currentX = evt.getX(); currentY = evt.getY(); oldX = currentX; oldY = currentY; System.out.println(currentX + " " + currentY); System.out.println("PEN!!!!");        }    }    private void jPanel2MousePressed(MouseEvent evt) {        oldX = evt.getX();        oldY = evt.getY();        System.out.println(oldX + " " + oldY);    }    //mouse released//    private void jPanel2MouseReleased(MouseEvent evt) {        if (tool == 2) { currentX = evt.getX(); currentY = evt.getY(); System.out.println("line!!!! from" + oldX + "to" + currentX);        }    }    //set ui visible//    public static void main(String args[]) {        EventQueue.invokeLater(new Runnable() { public void run() {     new JavaPaintUI().setVisible(true); }        });    }    // Variables declaration - do not modify    private JPanel jPanel2;    // End of variables declaration    // This class name is very confusing, since it is also used as the    // name of an attribute!    //class jPanel2 extends JPanel {    class Panel2 extends JPanel {        Panel2() { // set a preferred size for the custom panel. setPreferredSize(new Dimension(420,420));        }        @Override        public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("BLAH", 20, 20); g.drawRect(200, 200, 200, 200);        }    }}


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

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

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