NetBeans 创建画图程序

来源:互联网 发布:vivo手机软件网络错误 编辑:程序博客网 时间:2024/05/23 21:26

    在netBeans中并没有直接提供画图的JPanel,我们需要自定义一个类来重写paintComponent方法。

    我们需要自定义一个类继承JPanel,并重写paintComponent方法

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package papp;import java.awt.Color;import java.awt.Graphics;import java.util.List;/** * * @author user */public class DrawPanel extends javax.swing.JPanel{    public DrawPanel()    {        initComponents();    }        private void initComponents() {        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);        this.setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 400, Short.MAX_VALUE)        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 300, Short.MAX_VALUE)        );    }    public List<Pxy> pxyList;    protected void paintComponent(Graphics g)    {        if(pxyList != null)        {             Color c = g.getColor();            for(int i=0;i<pxyList.size();i++)            {                Pxy pxy = pxyList.get(i);                g.drawString(pxy.getId()+"", pxy.getX(), pxy.getY());                g.fillOval(pxy.getX(), pxy.getY(), 5, 5);                g.setColor(c);;            }        }           }}

上面就是一个将一系列的点画到面板上去的一个自定义的类

     上面添加的一些其他方法,是copy  JPanel模版的方法,要查看JPanel模版的代码。可以从   工具-》模版-》Swing GUI窗体-》JPanel窗体  选择下面的按钮,在编辑器打开

可以查看到模版代码:

<#assign licenseFirst = "/*"><#assign licensePrefix = " * "><#assign licenseLast = " */"><#include "../Licenses/license-${project.license}.txt"><#if package?? && package != "">package ${package};</#if>/** * * @author ${user} */public class ${name} extends javax.swing.JPanel {    /** Creates new form ${name} */    public ${name}() {        initComponents();    }    /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    @SuppressWarnings("unchecked")    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                              private void initComponents() {        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);        this.setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 400, Short.MAX_VALUE)        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 300, Short.MAX_VALUE)        );    }    // </editor-fold>                            // Variables declaration - do not modify                         // End of variables declaration                   }

写好自定义的类之后,在工程中选中这个类,然后选择 工具-》添加到组件面板-》swing容器

然后切换到设计模式下,就可以看到组件面板多了一个自定义的面板,直接拖上去就可以做界面了,如果报错,就请参考模版的代码。





原创粉丝点击