swing - Fickle JMenuBar -
i ran following code 10 times. of 10 runs, 3 showed both menu bar , rectangle, 3 showed rectangle, , 4 showed nothing @ all. doing wrong?
import java.awt.*; import java.awt.event.*; import javax.swing.*; import static java.awt.color.*; import java.awt.image.*; public class gui extends jframe implements keylistener, actionlistener { int x, y; public static void main(string[] args) { new gui(); } public gui() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { e.printstacktrace(); } frameinit(); setsize(1024,768); setdefaultcloseoperation(exit_on_close);; setvisible(true); setjmenubar(createmenubar()); addkeylistener(this); createbufferstrategy(2); x = 0; y = 49; } public void paint(graphics gm) { bufferstrategy bs = getbufferstrategy(); try { graphics g = bs.getdrawgraphics(); super.paint(g); g.setcolor(white); g.drawrect(0,0,1024,768); g.setcolor(black); g.fillrect(x,y,100,100); bs.show(); }catch(exception e) { } } public jmenubar createmenubar() { jmenubar menubar = new jmenubar(); jmenu filemenu = new jmenu("file"); filemenu.setmnemonic(keyevent.vk_f); jmenuitem save = new jmenuitem("save"); save.setmnemonic(keyevent.vk_s); save.addactionlistener(this); jmenuitem load = new jmenuitem("load"); load.setmnemonic(keyevent.vk_l); load.addactionlistener(this); jmenuitem quit = new jmenuitem("quit"); quit.setmnemonic(keyevent.vk_q); quit.addactionlistener(this); filemenu.add(save); filemenu.add(load); filemenu.addseparator(); filemenu.add(quit); jmenu editmenu = new jmenu("edit"); editmenu.setmnemonic(keyevent.vk_e); jmenuitem undo = new jmenuitem("undo"); undo.setmnemonic(keyevent.vk_u); undo.addactionlistener(this); jmenuitem redo = new jmenuitem("redo"); redo.setmnemonic(keyevent.vk_r); redo.addactionlistener(this); editmenu.add(undo); editmenu.add(redo); jmenu helpmenu = new jmenu("help"); helpmenu.setmnemonic(keyevent.vk_h); jmenuitem controls = new jmenuitem("controls"); controls.setmnemonic(keyevent.vk_c); controls.addactionlistener(this); jmenuitem = new jmenuitem("about"); about.setmnemonic(keyevent.vk_a); about.addactionlistener(this); helpmenu.add(controls); helpmenu.addseparator(); helpmenu.add(about); menubar.add(filemenu); menubar.add(editmenu); menubar.add(helpmenu); menubar.setlocation(0,23); return menubar; } public void actionperformed(actionevent e) { system.out.println(e.getactioncommand()); repaint(); } public void keypressed(keyevent e) { if(e.getkeycode()==keyevent.vk_up) { y-=10; } if(e.getkeycode()==keyevent.vk_down) { y+=10; } if(e.getkeycode()==keyevent.vk_left) { x-=10; } if(e.getkeycode()==keyevent.vk_right) { x+=10; } repaint(); } public void keyreleased(keyevent e) { } public void keytyped(keyevent e) { } }
since you're overriding way jframe painted ignoring jmenu bar painting completely.
don't subclass jframe, use custom component, here's quick edit of code, without thinking it, i've move pieces.
it works 100% of times me:
it works fine http://img706.imageshack.us/img706/1291/capturadepantalla201001l.png
//i ran following code 10 times. of 10 runs, 3 showed both menu bar , rectangle, 3 showed rectangle, , 4 showed nothing @ all. doing wrong? import java.awt.*; import java.awt.event.*; import javax.swing.*; import static java.awt.color.*; import java.awt.image.*; public class gui extends jcomponent implements keylistener, actionlistener { private int x, y; public static void main(string[] args) { setlnf(); // don't subclass use it. jframe frame = new jframe(); //frameinit(); frame.setsize(1024,768); frame.setdefaultcloseoperation(jframe.exit_on_close); //frame.setvisible(true); gui gui = new gui(); frame.setjmenubar(gui.createmenubar()); frame.addkeylistener( gui ); frame.add( gui ) ; frame.setvisible(true); // should last thing call } private static void setlnf() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception e) { e.printstacktrace(); } catch( instantiationexception ie ) { ie.printstacktrace(); } catch( illegalaccessexception iae ) { iae.printstacktrace(); } catch( unsupportedlookandfeelexception u ) { u.printstacktrace(); } } public gui() { //createbufferstrategy(2); x = 0; y = 49; } public void paint(graphics gm) { //bufferstrategy bs = getbufferstrategy(); try { graphics g = gm; // bs.getdrawgraphics(); super.paint(g); g.setcolor(white); g.drawrect(0,0,1024,768); g.setcolor(black); g.fillrect(x,y,100,100); //bs.show(); }catch(exception e) { e.printstacktrace(); } } public jmenubar createmenubar() { jmenubar menubar = new jmenubar(); jmenu filemenu = new jmenu("file"); filemenu.setmnemonic(keyevent.vk_f); jmenuitem save = new jmenuitem("save"); save.setmnemonic(keyevent.vk_s); save.addactionlistener(this); jmenuitem load = new jmenuitem("load"); load.setmnemonic(keyevent.vk_l); load.addactionlistener(this); jmenuitem quit = new jmenuitem("quit"); quit.setmnemonic(keyevent.vk_q); quit.addactionlistener(this); filemenu.add(save); filemenu.add(load); filemenu.addseparator(); filemenu.add(quit); jmenu editmenu = new jmenu("edit"); editmenu.setmnemonic(keyevent.vk_e); jmenuitem undo = new jmenuitem("undo"); undo.setmnemonic(keyevent.vk_u); undo.addactionlistener(this); jmenuitem redo = new jmenuitem("redo"); redo.setmnemonic(keyevent.vk_r); redo.addactionlistener(this); editmenu.add(undo); editmenu.add(redo); jmenu helpmenu = new jmenu("help"); helpmenu.setmnemonic(keyevent.vk_h); jmenuitem controls = new jmenuitem("controls"); controls.setmnemonic(keyevent.vk_c); controls.addactionlistener(this); jmenuitem = new jmenuitem("about"); about.setmnemonic(keyevent.vk_a); about.addactionlistener(this); helpmenu.add(controls); helpmenu.addseparator(); helpmenu.add(about); menubar.add(filemenu); menubar.add(editmenu); menubar.add(helpmenu); menubar.setlocation(0,23); return menubar; } public void actionperformed(actionevent e) { system.out.println(e.getactioncommand()); repaint(); } public void keypressed(keyevent e) { if(e.getkeycode()==keyevent.vk_up) { y-=10; } if(e.getkeycode()==keyevent.vk_down) { y+=10; } if(e.getkeycode()==keyevent.vk_left) { x-=10; } if(e.getkeycode()==keyevent.vk_right) { x+=10; } repaint(); } public void keyreleased(keyevent e) { } public void keytyped(keyevent e) { } }
edit
your problem here:
frameinit(); setsize(1024,768); setdefaultcloseoperation(exit_on_close);; setvisible(true); //<-- exaaaactly here!! setjmenubar(createmenubar()); addkeylistener(this); createbufferstrategy(2);
just move line end , should work fine.
frameinit(); setsize(1024,768); setdefaultcloseoperation(exit_on_close);; ///setvisible(true); //<-- exaaaactly here!! setjmenubar(createmenubar()); addkeylistener(this); createbufferstrategy(2); setvisible( true );
what said subclassing jframe still valid.
Comments
Post a Comment