如果构造函数已明确定义,则构造函数未定义

时间:2013-01-31 16:23:55

标签: java

我正在制作一个绘制原子衰变的程序。这是我的主要内容,也是逻辑。但是,当它在另一个类中明确定义时,我得到一个未定义的构造函数错误。为什么会这样?

警告:没有注明。把你的愤怒饶了我。

import java.util.Random;
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;

public class Main {
    public static void main(String args[]) {
        int chance = 6;
        Random r = new Random();
        int num = 40;
        int[] decayed;
        int reps = 25;
        decayed = new int[reps];
        for (int j = 1; j < reps+1; j++) {
            for (int i = 0; i < num; i++) {
                int c = r.nextInt(chance);
                if (c == chance - 1) {
                    decayed[j]++;
                }
            }
            System.out.printf("\n Trial: " + j + "\n Number left: " + num
                    + "\n Decayed: " + decayed[j] + "\n\n");
            num = num - decayed[j];

        }
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new Graph(decayed[])); //"Constuctor is undefined for type int" When I am clearly specifying an array.
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

我的Graph.class。它是从一些论坛复制的(Credit to Crieg Wood)。

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Graph extends JPanel
{
    int PAD = 20;
    boolean drawLine = true;
    boolean drawDots = true;
    int dotRadius = 3;

    // the y coordinates of the points to be drawn; the x coordinates are evenly spaced
    int[] data;
    public Graph(int points[]){ //This is the constructor which specifies type int[].
        for (int i = 0; i<points.length; i++){ //Copies points[] to data[]
            data[i] = points[i];
        }
    }

    protected void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        g2.drawLine(PAD, PAD, PAD, h-PAD);
        g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
        double xScale = (w - 2*PAD) / (data.length + 1);
        double maxValue = 100.0;
        double yScale = (h - 2*PAD) / maxValue;
        // The origin location
        int x0 = PAD;
        int y0 = h-PAD;

        // draw connecting line
        if (drawLine)
        {
            for (int j = 0; j < data.length-1; j++)
            {
                int x1 = x0 + (int)(xScale * (j+1));
                int y1 = y0 - (int)(yScale * data[j]);
                int x2 = x0 + (int)(xScale * (j+2));
                int y2 = y0 - (int)(yScale * data[j+1]);
                g2.drawLine(x1, y1, x2, y2);
            }
        }

        // draw the points as little circles in red
        if (drawDots)
        {
            g2.setPaint(Color.red);
            for (int j = 0; j < data.length; j++)
            {
                int x = x0 + (int)(xScale * (j+1));
                int y = y0 - (int)(yScale * data[j]);
                g2.fillOval(x-dotRadius, y-dotRadius, 2*dotRadius, 2*dotRadius);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:8)

这里的问题是使用那些[]括号。

尝试重新拨打电话:

f.getContentPane().add(new Graph(decayed));

虽然您没有错,但请考虑重写您的构造函数以遵守Java标准和约定:

public Graph(int[] points){   // NOTE: I moved the [] to a the standard position
    for (int i = 0; i<points.length; i++){
        data[i] = points[i];
    }
}

答案 1 :(得分:4)

您的语法无效,要引用您的数组,只需使用变量名称,而不使用[]

f.getContentPane().add(new Graph(decayed));

答案 2 :(得分:1)

只需替换此f.getContentPane().add(new Graph(decayed[]));

即可

用这个

f.getContentPane().add(new Graph(decayed));

只使用您创建的变量的名称,而不是[]

那些[]括号仅在声明数组的方法参数时使用,而不是在调用方法时使用。