java - How do you send call to main class from a class file? -
hi have 1 main program file 2 class files involved in this. in main class file create frame , control how interact. 1 of class files needs talk main class file change 3rd file. trying go find out how call sent back.
the main class file:
public class mainframe extends javax.swing.jframe { public mainframe() { buttonpanel1 = new buttonpanel(); textpanel1 = new textpanel(); textpanel2 = new textpanel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false) .addcomponent(buttonpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(textpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(textpanel2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value))) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(buttonpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(textpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(textpanel2, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) ); pack(); } public static void main(string args[]) { java.awt.eventqueue.invokelater(() -> { new mainframe().setvisible(true); }); } public void copytext() { textpanel2.settext(textpanel1.gettext()); } private buttonpanel buttonpanel1; private textpanel textpanel1; private textpanel textpanel2;
}
the button class file tries call main file. change 3rd file.
public class buttonpanel extends javax.swing.jpanel { public buttonpanel() { jbutton1 = new javax.swing.jbutton(); jbutton1.settext("change"); jbutton1.addactionlistener((java.awt.event.actionevent evt) -> { //i tell main frame textpanel1 , put textpanel2 mainframe.copytext(); }); javax.swing.grouplayout layout = new javax.swing.grouplayout(this); this.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup().addcomponent(jbutton1)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup().addcomponent(jbutton1)) ); } private javax.swing.jbutton jbutton1; }
and 1 being changed:
public class textpanel extends javax.swing.jpanel { public textpanel() { jtextfield1 = new javax.swing.jtextfield(); jtextfield1.settext("hello"); javax.swing.grouplayout layout = new javax.swing.grouplayout(this); this.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcomponent(jtextfield1, javax.swing.grouplayout.default_size, 59, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jtextfield1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) ); } public string gettext(){ return jtextfield1.gettext(); } public void settext(string str){ jtextfield1.settext(str); } private javax.swing.jtextfield jtextfield1; }
if make method static, work, won't able directly access object.
mainframe.copytext();
otherwise, pass reference mainframe buttonpanel. example:
public buttonpanel(mainframe mainframe) { this.mainframe = mainframe; ... }
and create on mainframe:
new buttonpanel(this);
Comments
Post a Comment