java - How do I transition between these GUI frames? -


so, have java program here , it's choose own adventure game. @ each step of game, user has choice between 2 options, "one" or "two" , options branch off different parts of story.

for example: female college student named angelina jackson on break studying. @ favorite neighborhood cafe 1 day, when bump stranger , spill coffee on both of you! do? option one: apologize being clumsy. option two: you're furious because shoes brand new. turn around , prepare give him scolding of life!

currently, program runs text in eclipse. i'm trying make entire game appear on gui shown in picture below. problem can't transition between different frames of project, there multiple frames 1 in picture below.

how can connect frames "one" , "two" buttons? when 1 button pressed, transition next frame. i'm not sure how work that.

enter image description here

this code first instance of our game (as seen in picture):

import java.awt.borderlayout; import java.awt.dimension; import java.awt.flowlayout; import java.awt.font; import java.awt.gridlayout;  import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea;   public class samplewindow extends jframe{ private jtextarea text; private jbutton one, two;  public samplewindow(){     super("0");      font myfont = new font("serif", font.plain, 18);      jpanel panel = new jpanel();     panel.setlayout(new flowlayout());     jpanel panel2 = new jpanel();     panel.setlayout(new flowlayout());      jtextarea text = new jtextarea();     text =  new jtextarea("you female college student named angelina jackson \n"             + "who on break studying. \n"             + "you @ favorite neighborhood cafe 1 day, \n"              + "when bump stranger , spill coffee on both of you! \n"              + "what do?\n"             + "option one: apologize being clumsy. \n"             + "option two: you're furious because shoes brand new. \n"             + "you turn around , prepare give him scolding of life! \n");     text.setfont(myfont);     text.seteditable(false);      1 = new jbutton("option one: ");     2 = new jbutton("option two: ");      panel.add(text);     panel2.add(one);     panel2.add(two);      add(panel, borderlayout.north);      add(panel2, borderlayout.center);    }  public static void main(string[] args) {     samplewindow sample = new samplewindow();     sample.setdefaultcloseoperation(jframe.exit_on_close);     sample.setsize(600, 350); //top side     sample.setvisible(true);     }  } 

and here code second instance:

import java.awt.borderlayout; import java.awt.dimension; import java.awt.flowlayout; import java.awt.font; import java.awt.gridlayout;  import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea;   public class samplewindow extends jframe{ private jtextarea text; private jbutton one, two;  public samplewindow(){     super("0");      font myfont = new font("serif", font.plain, 18);      jpanel panel = new jpanel();     panel.setlayout(new flowlayout());     jpanel panel2 = new jpanel();     panel.setlayout(new flowlayout());      jtextarea text = new jtextarea();     text =  new jtextarea("the guy angry, it's starting little scary.\n"             + "you decide you'd better out of there before gets hurt.\n"             + "where go next?\n"             + "option one: go home , finish math homework.\n"             + "option two: go bank , cash can go shopping.\n"             + "please enter \"one\" first option, , \"two\" second option.");     text.setfont(myfont);     text.setfont(myfont);     text.seteditable(false);      1 = new jbutton("option one: ");     2 = new jbutton("option two: ");      panel.add(text);     panel2.add(one);     panel2.add(two);      add(panel, borderlayout.north);      add(panel2, borderlayout.center);    }  public static void main(string[] args) {     samplewindow sample = new samplewindow();     sample.setdefaultcloseoperation(jframe.exit_on_close);     sample.setsize(600, 350); //top side     sample.setvisible(true);     }  } 

how link frames through use of jbutton? thank you!

how link frames through use of jbutton?

don't swap jframes no need , further user interface better served simplifying. instead best solution create 1 jframe displays jtextarea , change text displayed in jtextarea holds.

this make code lot simpler , shorter, , save user fatigue of seeing swapping windows, see in professional applications because can annoying user.

myself, i'd put text in text file , not in program, separate displayed strings separated perhaps 2 empty lines, i'd have program read in lines 2 (or more) strings, , on button push, swap text in jtextarea calling settext(...) on it, passing in new string.

for example uses hashmap<string, string> swap text (but not necessary).

import java.awt.borderlayout; import java.awt.font; import java.awt.gridlayout; import java.awt.event.actionevent; import java.util.hashmap; import java.util.map;  import javax.swing.*;  @suppresswarnings("serial") public class swaptext extends jpanel {    private static final int rows = 25;    private static final int cols = 40;    private static final string abe_lincoln = "abraham lincoln's gettysburg address";    private static final string chief_joseph = "chief joseph's surrender speech";    private static final font txt_area_font = new font("serif", font.plain, 18);    private jtextarea textarea = new jtextarea(rows, cols);    private map<string, string> optiontextmap = new hashmap<>();     public swaptext() {       // add text map       optiontextmap.put(abe_lincoln,                   "four score , 7 years ago our fathers brought forth, "                         + "upon continent, new nation, conceived in liberty, , "                         + "dedicated proposition men created"                         + " equal.\n\n"                         +                          "now engaged in great civil war, testing whether "                         + "that nation, or nation conceived, , dedicated, "                         + "can long endure. "                         + "we met here on great battlefield of war. "                         + "we have come dedicate portion of it, final resting place "                         + "here gave lives that nation might live. "                         + "it altogether fitting , proper should this.\n\n"                         +                          "but in larger sense, can not dedicate — can not "                         + "consecrate — can not hallow — ground. brave men, living , dead, "                         + "who struggled here, have consecrated far above our "                         + "poor power add or detract. world little note, nor long remember, "                         + "what here, can never forget did here.\n\n"                         +                          "it us, living, rather dedicated here "                         + "to unfinished work have, far, nobly carried on. "                         + "rather here dedicated great task "                         + "remaining before — these honored dead take increased devotion "                         + "to cause gave last full measure of "                         + "devotion – here highly resolve these dead shall not have died in "                         + "vain; nation shall have new birth of freedom; "                         + "and government of people, people, people, "                         + "shall not perish earth.");       optiontextmap.put(chief_joseph,                   "i tired of fighting.  our chiefs killed.  looking glass dead.  toohulhulsote dead.  "                         + "the old men dead.  young men yes or no. led young men dead.\n\n"                         + "it cold , have no blankets.  little children freezing death.  "                         + "my people, of them, have run away hills , have no blankets, "                         + "no food.  no 1 knows are--perhaps freezing death.  "                         + "i want have time children , see how many can find.  "                         + "maybe shall find them among dead. \n\n"                         + "hear me, chiefs.  tired.  heart sick , sad.  "                         + "from sun stands, fight no more forever.");        // allow text area wrap lines automatically       textarea.setlinewrap(true);       textarea.setwrapstyleword(true);       textarea.setfocusable(false); // folks can't edit text       textarea.setfont(txt_area_font);        // place text area in jscrollpane       jscrollpane scrollpane = new jscrollpane(textarea);       scrollpane             .setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);        jpanel buttonpanel = new jpanel(new gridlayout(1, 0, 5, 0));       buttonpanel.add(new jbutton(new optionaction(abe_lincoln)));       buttonpanel.add(new jbutton(new optionaction(chief_joseph)));        // use borderlayout main jpanel , add components       setlayout(new borderlayout());       add(scrollpane, borderlayout.center);       add(buttonpanel, borderlayout.page_end);    }     private class optionaction extends abstractaction {       public optionaction(string name) {          super(name);       }        @override       public void actionperformed(actionevent e) {          textarea.settext(optiontextmap.get(getvalue(name)));       }    }     private static void createandshowgui() {       swaptext mainpanel = new swaptext();        jframe frame = new jframe("swap text");       frame.setdefaultcloseoperation(jframe.dispose_on_close);       frame.getcontentpane().add(mainpanel);       frame.pack();       frame.setlocationbyplatform(true);       frame.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -