java - Can't add a panel (so background) to my JFrame -
i have jpanel use background name backgroundpanel. , designing main jframe netbeans, , adding backgroundpanel that.
public class main extends javax.swing.jframe { image img = null; public main() { initcomponents(); settitle("count to"); setpreferredsize(new dimension(800, 600)); image img = null; try { img = imageio.read(new file("resources/bg.png")); } catch (ioexception e) {system.out.println("image can't found.");} backgroundpanel bgpanel = new backgroundpanel(img, backgroundpanel.scaled, 0.0f, 0.0f); setdefaultcloseoperation(jframe.exit_on_close); getcontentpane().add(bgpanel); pack(); setvisible(true); //setresizable(false); setlocationrelativeto(null); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); setcursor(new java.awt.cursor(java.awt.cursor.default_cursor)); setforeground(java.awt.color.red); seticonimage(geticonimage()); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 400, short.max_value) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 300, short.max_value) ); pack(); }// </editor-fold> /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(main.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(main.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(main.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(main.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new main().setvisible(true); } }); } // variables declaration - not modify // end of variables declaration }
but backgroundpanel not visible, there no background image of frame, plain gray (which default).
i hope explanation clear. can include backgroundpanel class if need it.
all top level containers (and jinternalframe
) have what's called "root pane". responsible building primary layout of window.
the root pane consists (basically) of content pane , glass (and option menu bar), sit on top of window.
when add components window, been added content pane.
in case, makes sense replace content pane own panel rather trying add existing one.
this way, when add new components window, added backgroundpane
instead.
make sure though, change content pane before try adding components window ;)
check out how use root panes more details
Comments
Post a Comment