我有这段代码,但JPanel
重绘方法不起作用。如果我在将它添加到 JFrame 类后这样做,我会对其进行调试,但它不记得了paintComponent
。
为什么呢?
package trial;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class jp extends JPanel
{
int i=0;
{
setBackground(Color.RED);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Color[] c={Color.red,Color.green};
//To change body of generated methods, choose Tools | Templates.
g.drawLine(i, i, i+100, i+100);
i+=50;
}
}
public class Trial extends JFrame{
public static void main(String[] args) {
Trial f=new Trial();
jp a;
a=new jp();
System.out.println(a.i);
f.add(a);
f.setVisible(true);
f.setSize(500, 500);
f.setDefaultCloseOperation(3);
a.repaint();
}
}