I'm a beginner in java so sorry if the question seems a bit stupid
I have this simple code but I do not understand some of the coding.
I know that the keyword this refers to this class but i still dont understand exactly why is needed at that point .
For example :
public class SquareSimp
{
public static void main( String[] args )
{
FilledFrame frame = new FilledFrame();
frame.setVisible( true );
}
}
class FilledFrame extends JFrame
{
int size = 400;
public FilledFrame()
{
JButton butSmall = new JButton("Small");
JButton butMedium = new JButton("Medium");
JButton butLarge = new JButton("Large");
JButton butMessage = new JButton("Say Hi!");
SquarePanel panel = new SquarePanel(this); WHERE DOES (THIS) REFER TO
AND WHY DO WE NEED IT?
JPanel butPanel = new JPanel();
butPanel.add(butSmall);
butPanel.add(butMedium);
butPanel.add(butLarge);
butPanel.add(butMessage);
add(butPanel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setSize( size+100, size+100 );
}
}
class SquarePanel extends JPanel
{
FilledFrame theApp; // WHY DO WE CREATE A VARIABLE OF A TYPE CLASS? HERE?
SquarePanel(FilledFrame app)
{
theApp = app;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.green);
g.fillRect(20, 20, theApp.size, theApp.size);
}
}