java - How to "save" my program? -
hi guys i'm working on kind of "dungeon crawler" game , need saving program. jframe save(all of entered text)so when close program , open desktop have entered text , have changed still in program. i'm confused how save , hoping help. ahead of time of time, anwsers, links, examples, , advice. clear new , have been coding 5 days , still use netbeans jframe "design" feature generate of code me. below i've posted code second jframe. feel free copy , paste code in netbeans ide. :).
note: in case needing names other jframes. "character" second jframe "mainclass" java app "maingamepanel" third jframe
note:some buttons may not work because not of jframes
package ddsheet; //this code jframe named character import java.nio.file.files; import javax.swing.jfilechooser; public class character extends javax.swing.jframe { public character() { initcomponents(); } /** * 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() { jpanel1 = new javax.swing.jpanel(); jlabel1 = new javax.swing.jlabel(); jlabel2 = new javax.swing.jlabel(); jlabel3 = new javax.swing.jlabel(); jlabel4 = new javax.swing.jlabel(); jlabel5 = new javax.swing.jlabel(); jlabel6 = new javax.swing.jlabel(); jlabel7 = new javax.swing.jlabel(); jlabel8 = new javax.swing.jlabel(); jlabel9 = new javax.swing.jlabel(); jlabel10 = new javax.swing.jlabel(); jbutton1 = new javax.swing.jbutton(); jscrollpane1 = new javax.swing.jscrollpane(); jtextarea1 = new javax.swing.jtextarea(); jlabel11 = new javax.swing.jlabel(); namevalue = new javax.swing.jlabel(); jlabel13 = new javax.swing.jlabel(); jlabel14 = new javax.swing.jlabel(); strvalue = new javax.swing.jlabel(); int1value = new javax.swing.jlabel(); dexvalue = new javax.swing.jlabel(); vitvalue = new javax.swing.jlabel(); hpvalue = new javax.swing.jlabel(); mpvalue = new javax.swing.jlabel(); stamvalue = new javax.swing.jlabel(); jprogressbar1 = new javax.swing.jprogressbar(); jlabel12 = new javax.swing.jlabel(); class = new javax.swing.jlabel(); jmenubar1 = new javax.swing.jmenubar(); jmenu1 = new javax.swing.jmenu(); jmenuitem1 = new javax.swing.jmenuitem(); jmenuitem2 = new javax.swing.jmenuitem(); jmenu2 = new javax.swing.jmenu(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jpanel1.setborder(javax.swing.borderfactory.createtitledborder("character")); jlabel1.settext("name:"); jlabel2.settext("level:"); jlabel3.settext("gold:"); jlabel4.settext("str:"); jlabel5.settext("int:"); jlabel6.settext("dex:"); jlabel7.settext("vit:"); jlabel8.settext("hp:"); jlabel9.settext("mp"); jlabel10.settext("stam:"); jbutton1.settext("main menu"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); jtextarea1.setcolumns(20); jtextarea1.setrows(5); jscrollpane1.setviewportview(jtextarea1); jlabel11.settext("inventory"); namevalue.settext("hiroshema"); jlabel13.settext("1"); jlabel14.settext("550"); strvalue.settext("0"); int1value.settext("0"); dexvalue.settext("0"); vitvalue.settext("0"); hpvalue.settext("0"); mpvalue.settext("0"); stamvalue.settext("0"); jlabel12.settext("exp:"); class.settext("class"); javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); jpanel1.setlayout(jpanel1layout); jpanel1layout.sethorizontalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addcontainergap() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel1layout.createsequentialgroup() .addgap(0, 0, short.max_value) .addcomponent(jbutton1) .addgap(35, 35, 35)) .addgroup(jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(jlabel1) .addgap(18, 18, 18) .addcomponent(namevalue)) .addcomponent(jlabel4) .addcomponent(jlabel7) .addcomponent(jlabel9) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(jlabel5) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(int1value, javax.swing.grouplayout.preferred_size, 41, javax.swing.grouplayout.preferred_size)) .addcomponent(jlabel8) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false) .addgroup(javax.swing.grouplayout.alignment.leading, jpanel1layout.createsequentialgroup() .addcomponent(jlabel6) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 11, short.max_value) .addcomponent(dexvalue, javax.swing.grouplayout.preferred_size, 46, javax.swing.grouplayout.preferred_size)) .addcomponent(strvalue, javax.swing.grouplayout.preferred_size, 47, javax.swing.grouplayout.preferred_size) .addcomponent(vitvalue, javax.swing.grouplayout.preferred_size, 49, javax.swing.grouplayout.preferred_size) .addcomponent(hpvalue, javax.swing.grouplayout.preferred_size, 48, javax.swing.grouplayout.preferred_size) .addcomponent(mpvalue, javax.swing.grouplayout.preferred_size, 51, javax.swing.grouplayout.preferred_size) .addgroup(javax.swing.grouplayout.alignment.leading, jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addgroup(javax.swing.grouplayout.alignment.leading, jpanel1layout.createsequentialgroup() .addcomponent(jlabel3) .addgap(18, 18, 18) .addcomponent(jlabel14)) .addgroup(javax.swing.grouplayout.alignment.leading, jpanel1layout.createsequentialgroup() .addcomponent(jlabel2) .addgap(18, 18, 18) .addcomponent(jlabel13))) .addgap(0, 0, short.max_value)) .addgroup(jpanel1layout.createsequentialgroup() .addcomponent(jlabel10) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(stamvalue, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)))) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 90, short.max_value) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 233, javax.swing.grouplayout.preferred_size) .addcomponent(jlabel11)) .addgap(97, 97, 97)) .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel1layout.createsequentialgroup() .addcomponent(class) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jlabel12) .addgap(18, 18, 18) .addcomponent(jprogressbar1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap()))) ); jpanel1layout.setverticalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jprogressbar1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(jlabel12)) .addgap(26, 26, 26)) .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel1layout.createsequentialgroup() .addcomponent(class) .addgap(18, 18, 18))) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel1) .addcomponent(namevalue) .addcomponent(jlabel11)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanel1layout.createsequentialgroup() .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel2) .addcomponent(jlabel13)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel3) .addcomponent(jlabel14)) .addgap(41, 41, 41) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel4) .addcomponent(strvalue)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel5) .addcomponent(int1value)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel6) .addcomponent(dexvalue)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel7) .addcomponent(vitvalue)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel8) .addcomponent(hpvalue)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel9) .addcomponent(mpvalue)) .addgap(18, 18, 18) .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel10) .addcomponent(stamvalue)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton1)) .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 283, javax.swing.grouplayout.preferred_size)) .addcontainergap(javax.swing.grouplayout.default_size, short.max_value)) ); jmenu1.settext("file"); jmenuitem1.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_e, java.awt.event.inputevent.ctrl_mask)); jmenuitem1.settext("exit"); jmenuitem1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jmenuitem1actionperformed(evt); } }); jmenu1.add(jmenuitem1); jmenuitem2.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_s, java.awt.event.inputevent.ctrl_mask)); jmenuitem2.settext("save"); jmenuitem2.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jmenuitem2actionperformed(evt); } }); jmenu1.add(jmenuitem2); jmenubar1.add(jmenu1); jmenu2.settext("edit"); jmenubar1.add(jmenu2); setjmenubar(jmenubar1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcontainergap()) ); pack(); }// </editor-fold> private void jmenuitem1actionperformed(java.awt.event.actionevent evt) { system.exit(0); } private void jbutton1actionperformed(java.awt.event.actionevent evt) { this.dispose(); new maingamepanel().setvisible(true); } private void jmenuitem2actionperformed(java.awt.event.actionevent evt) { } /** * @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(character.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(character.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(character.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(character.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 character().setvisible(true); } }); } // variables declaration - not modify public static javax.swing.jlabel class; public static javax.swing.jlabel dexvalue; public static javax.swing.jlabel hpvalue; public static javax.swing.jlabel int1value; private javax.swing.jbutton jbutton1; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel10; private javax.swing.jlabel jlabel11; private javax.swing.jlabel jlabel12; private javax.swing.jlabel jlabel13; private javax.swing.jlabel jlabel14; private javax.swing.jlabel jlabel2; private javax.swing.jlabel jlabel3; private javax.swing.jlabel jlabel4; private javax.swing.jlabel jlabel5; private javax.swing.jlabel jlabel6; private javax.swing.jlabel jlabel7; private javax.swing.jlabel jlabel8; private javax.swing.jlabel jlabel9; private javax.swing.jmenu jmenu1; private javax.swing.jmenu jmenu2; private javax.swing.jmenubar jmenubar1; private javax.swing.jmenuitem jmenuitem1; private javax.swing.jmenuitem jmenuitem2; private javax.swing.jpanel jpanel1; private javax.swing.jprogressbar jprogressbar1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jtextarea jtextarea1; public static javax.swing.jlabel mpvalue; public static javax.swing.jlabel namevalue; public static javax.swing.jlabel stamvalue; public static javax.swing.jlabel strvalue; public static javax.swing.jlabel vitvalue; // end of variables declaration }
another idea create separate class containing information need save. , use class object in main class , save object this.
look serializing class (this called serialization).
public class gamestate implements serializable{ ...//game settings , states stored here } //inside main running class my_settings = gamestate(); ... // write object fileoutputstream fos = new fileoutputstream(path..); objectoutputstream out = new objectoutputstream(fos); out.writeobject( my_settings ); out.close(); fos.close(); //read object fis = new fileinputstream(path..); objectinputstream ois = new objectinputstream(fis); my_settings = (gamestate) ois.readobject(); ois.close();
Comments
Post a Comment