我在其他JFrame上有这段代码:
test2 diagram = new test2(headers,inputSize);
diagram.setVisible(true);
这段代码用于调用此JFrame,其代码为:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.util.ArrayList;
public class test2 extends javax.swing.JFrame {
static ArrayList<String> headers = new ArrayList<String>();
static int inputSize;
public test2 (ArrayList<String> headers, int inputSize)
{
initComponents();
this.headers = headers;
this.inputSize = inputSize;
}
public test2() {
initComponents();
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.black);
int x = 50;
int y = 60;
int st1 = 48;
for(int a=0;a<inputSize;a++)
{
System.out.println("a"+a);
g2.drawString(headers.get(a), st1, 55);
g2.draw(new Line2D.Double(x, y, x, 450));
x = x+30;
st1 = st1+30;
}
for(int ctr=0;ctr<2;ctr++)
{
System.out.println(ctr);
}
我已将paint()更改为paintComponents()。问题是它不执行或查看循环的输出,这意味着它不会进入循环。我使用Graphics2D还有其他方法来创建一条线吗?
答案 0 :(得分:0)
首先,要覆盖的正确方法是paintComponent(...)
而不是paint(...)
。
在此处获取更多信息: paint() in java applet is called twice for no reason