So, I made a program that draws a rectangle whenever a button is clicked. But the rectangle only shows up sometime.. Here is the code for JFrame
JFrame jf; // This is declared as a static member
JPanel panel = new JPanel();
panel.setBounds(200,0,200,400);
jf = new JFrame("Try");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setSize(400,400);
jf.setVisible(true);
JButton jb = new JButton("Click");
jb.setBounds(10,10,100,60);
panel.add(jb);
jf.add(panel);
Handler handle = new Handler();
jb.addActionListener(handle);
For ActionListener :
public void actionPerformed(ActionEvent ae)
{
Mainting maint = new Mainting();
jf.add(maint);
maint.draw(true);
And the drawing class: Boolean checker =false;
public void draw(Boolean b)
{
checker = b;
repaint();
}
@Override
public void paintComponent(Graphics g)
{
if(checker){
super.paintComponent(g);
g.fillRect(0,0,100,80);
The problem is : If I first maximize the window and then click on the button, nothing appears. If I just click the button, nothing appears. But if I click the button and maximize the window, a rectangle appears. Why this is happening ?