swing - How do I pass a value JFrame to JFrame by click of a button in Java? -
i need pass value jframe
jframe
click
of button. i'm new programming. tried everything. don't know how accomplish this.
here code -
import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jtextfield; import javax.swing.jlabel; import java.awt.flowlayout; public class demo{ private final jlabel showinput; private final jtextfield inputfield; public demo(){ jframe inputframe = new jframe("input"); jframe outputframe = new jframe("output"); inputfield = new jtextfield(15); jbutton button = new jbutton("click here"); } }); showinput = new jlabel("type in box , press button"); inputframe.getcontentpane().setlayout(new flowlayout()); inputframe.getcontentpane().add(inputfield); inputframe.getcontentpane().add(button); inputframe.pack(); inputframe.setdefaultcloseoperation(jframe.exit_on_close); outputframe.getcontentpane().setlayout(new flowlayout()); outputframe.getcontentpane().add(showinput); outputframe.pack(); outputframe.setdefaultcloseoperation(jframe.exit_on_close); inputframe.setvisible(true); outputframe.setvisible(true); } public static void main(string[] args){ demo example = new demo(); } }
try one.
import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jtextfield; import javax.swing.jlabel; import java.awt.flowlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; public class demo{ private final jlabel showinput; private final jtextfield inputfield; public demo(){ jframe inputframe = new jframe("input"); jframe outputframe = new jframe("output"); inputfield = new jtextfield(15); jbutton button = new jbutton("click here"); button.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string textfieldtext = inputfield.gettext(); showinput.settext(textfieldtext); } }); showinput = new jlabel("type in box , press button"); inputframe.getcontentpane().setlayout(new flowlayout()); inputframe.getcontentpane().add(inputfield); inputframe.getcontentpane().add(button); inputframe.pack(); inputframe.setdefaultcloseoperation(jframe.exit_on_close); outputframe.getcontentpane().setlayout(new flowlayout()); outputframe.getcontentpane().add(showinput); outputframe.pack(); outputframe.setdefaultcloseoperation(jframe.exit_on_close); inputframe.setvisible(true); outputframe.setvisible(true); } public static void main(string[] args){ demo example = new demo(); } }
i add these code function need.
/* button.addactionlistener(new actionlistener(){ // when button clicked, actionperformed called public void actionperformed(actionevent e){ string textfieldtext = inputfield.gettext(); // text showinput.settext(textfieldtext); // set text on jlabel } }); */
Comments
Post a Comment