java - How to use JButton with a class extending Canvas? -


i using class extends canvas in attempt make conway's game of life copy. not not new java new both swing , canvas. have tried many ways of adding jbutton objects canvas have had no success. have included code, , if has advice how implement buttons appreciated.

import java.awt.dimension; import java.awt.graphics;  import javax.swing.jbutton; import javax.swing.jframe;  public class conwaysgameoflife extends canvas implements runnable{     private static final long serialversionuid = 1l;      public static final int size = 960;      public static final string title = "conway's game of life";      private boolean running = false;     private thread thread;      private static jbutton but;      public static void main(string[] args)     {         conwaysgameoflife game = new conwaysgameoflife();          game.setpreferredsize(new dimension(size-10, size-10));         game.setmaximumsize(new dimension(size-10, size-10));         game.setminimumsize(new dimension(size-10, size-10));          jframe frame = new jframe(title);          frame.add(game);         frame.pack();         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setresizable(false);         frame.setlocationrelativeto(null);         frame.setvisible(true);          = new jbutton("button");         frame.add(but);          game.start();     }      private void start()     {         if(running)             return;          running = true;         thread = new thread(this);         thread.start();     }      private void stop()     {         if(!running)             return;          try{thread.join();}          catch(interruptedexception e){}         system.exit(1);     }      public void run()      {         while(running)         {             system.out.println("running");         }         stop();     }      public void paint(graphics g)     {         g.setcolor(color.black);         g.fillrect(0,0,size,64);          for(int = 16; < size; += 16)         {             g.drawline(i,0,i,size);             g.drawline(0,i,size,i);         }     } } 

the immediate problem you're having fact you've added components same position within borderlayout...

frame.add(game); // , me, i'm in center frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setlocationrelativeto(null); frame.setvisible(true);  = new jbutton("button"); frame.add(but);  // , me, i'm in center 

now, because you've not invalidated frame after adding button, it's not been updated yet, it's not been displayed, if did, might find have weird problems awt components don't have concept of z-depth, meaning might or might not cover button...fun stuff.

instead, add button different position within frame/borderlayout

frame.add(game); // , me, i'm in center = new jbutton("button"); frame.add(but, borderlayout.south); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setlocationrelativeto(null); frame.setvisible(true); 

take @ how use borderlayout more details.

i'd careful breaking paint chain of canvas without knowing more how painting done. should calling super.paint.

in fact, in case, there little benifit using canvas (and lot of problems), instead, should use jpanel , override it's paintcomponent method, making sure call super.paintcomponent before custom painting

see painting in awt , swing , performing custom painting more details


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -