NetBeans 创建画图程序

    在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容器

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





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值