我在我的另一个 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 有没有其他方法来创建一条线?