Swing:使用PaintComponent()的参数实例化一个类

时间:2013-11-14 03:37:11

标签: java swing constructor

我必须根据数组列表和来自不同类的整数在帧上绘制线条和字符串。我有以下代码:

public class test3 extends JPanel {

JFrame frame;
static ArrayList<String> headers = new ArrayList<>();
static int inputSize;

public test3 (ArrayList<String> headers, int inputSize)
{
    this.headers = headers;
    this.inputSize = inputSize;
}

public test3(){}

public static void main(String[] args)
{
    test3 g = new test3();
    g.setUpScreen();
}

public void setUpScreen()
{
    frame = new JFrame("Graphics Test");
    frame.setVisible(true);
    frame.setSize(700,512);
    frame.add(this);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2d = (Graphics2D)(g);
    int headersX=40;
    for(int ctr=0; ctr<headers.size(); ctr++)
    {
        g2d.drawString(headers.get(ctr), headersX, 30);
        g2d.drawLine(headersX, 40, headersX, 400);
        headersX+=30;
    }
}

}

我似乎无法获取数组列表和从另一个类传递的整数。我该如何解决这个问题?任何帮助将不胜感激,谢谢! :)

0 个答案:

没有答案