java - Why is this drawing a box before the button is pressed? -


i'm relatively new java , i'm trying code gui draws box when button pressed. problem is, program draws box before button pressed , don't know how fix this.

this controller class:

import java.awt.*; import javax.swing.*; import java.awt.event.* ;   public class testcontroller extends jframe { private jbutton enterbutton; private jpanel buttonholder; private container contentpane; private testview view;  public testcontroller() {     contentpane = getcontentpane();     enterbutton = new jbutton("enter");     buttonholder = new jpanel();     buttonholder.setpreferredsize(new dimension (600, 100));     contentpane.add(buttonholder, borderlayout.south);      buttonholder.add(enterbutton);      view= new testview();            view.setpreferredsize(new dimension (125, 125));     contentpane.add(view, borderlayout.center);      enterbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent ae) {             view.repaint();          }     });  } public static void main(string[] args) {      testcontroller bc = new testcontroller() ;      bc.setdefaultcloseoperation(jframe.exit_on_close) ;      bc.pack();      bc.setvisible(true) ;    }  } 

this view class:

import java.awt.* ;      import java.awt.geom.*; import javax.swing.* ;  public class testview extends jpanel { public testview() {}  public void paintcomponent( graphics g ){     super.paintcomponent( g );     graphics2d g2= ( graphics2d ) g;     rectangle2d rect= new rectangle2d.double(0, 0, 30, 30);      g2.setpaint( color.cyan );     g2.fill( rect ); } 

}

you have many ways it.

you can example set visibility of testview false until button pressed.

view.setvisible(false); 

and in button's action listener:

view.setvisible(true); 

why did have problem:

every visual object create visible default. when added object board, drawn because of that.

this call showed object: contentpane.add(view, borderlayout.center);


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -