package getcm;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test {
public static void main( String[] args ) {
tpanel panel = new tpanel();
JFrame app = new JFrame();
app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.setSize(250, 250);
app.setVisible(true);
}
}
class tpanel extends JPanel {
int st = 0;
public void paintComponent(Graphics g) {
for (int h = 0; h < 2; h++) {
System.out.println(st);
st += 1;
}
}
}
I think this result should be 0, 1 but in Eclipse, the data that printed is 0, 1 ,2 ,3.
I have a reason that variable st can not produce inside the fuction paintComponent, and I have to get result 0, 1. (getting paintComponent only one time)
Please help me to get result 0, 1.
I have no idea about why this code print 0, 1 ,2 ,3 not 0, 1.