我想用NetBeans编写界面画图程序,用户可在文本框中输入参数,点画图按钮后,程序会根据参数在面板上绘制矩形。绘图一般是在自定义的JPanel的派生类中重载paintComponent(Graphics comp)函数,可按钮和文本框在框架上而不在这个面板上, 想请教高手:1 怎样将用户输入的参数传给这个函数?2 怎样把这个面板的类型改为我定义的派生类名,而不是从组件面板中拖动过来后默认的JPanel类

解决方案 »

  1.   

    如果你要自定义JPanel的派生类中重载paintComponent(Graphics comp)函数,
    就不可以直接定义paintComponent(Graphics comp,int x ,int y) 之类的函数;
    因为这样就不可以重载了;
    所以这里我是在JPanel的派生类MyPanel中定义属性 int x;int y;int width;int hieght;
    利用属性的get和set的方法来实现参数的传递;具体程序:
    MyPanel类:
    import java.awt.*;
    import javax.swing.*;public class MyPanel extends JPanel {    private int x;
        private int y;
        private int height;
        private int width;    public MyPanel(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }    public void setheigth(int heigth) {
            this.height = heigth;
        }    public void setwidth(int width) {
            this.width = width;
        }    public void setx(int x) {
            this.x = x;
        }    public void sety(int y) {
            this.y = y;
        }    public int getx() {
            return this.x;
        }    public int gety() {
            return this.y;
        }    public int getheight() {
            return height;
        }    public int getwidth() {
            return width;
        }    @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(x, y, width, height);
        //g.drawr
        }
    }
      

  2.   

    实现窗体:import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class RectJFrame extends javax.swing.JFrame {    public RectJFrame() {
            initComponents();
            jp = new MyPanel(0, 0, 0, 0);
            jPanel1.add(jp);
        }    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
    //////////////////因为字数太多啦 唯有把设计窗体的函数删了
        private void initComponents() {}
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        //repaint();
        Graphics g = jPanel1.getGraphics();
        jp.setx(Integer.parseInt(jTextField1.getText()));
        jp.sety(Integer.parseInt(jTextField2.getText()));
        jp.setwidth(Integer.parseInt(jTextField3.getText()));
        jp.setheigth(Integer.parseInt(jTextField4.getText()));
        System.out.println(jp.getx() + " " + jp.gety() + " " + jp.getwidth() + " " + jp.getheight());
        jp.paintComponent(g);
    }private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }    /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new RectJFrame().setVisible(true);
                }
            });
        }    // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        // End of variables declaration
        private MyPanel jp;
    }