java - How to call a function of other class from another class -
i developing project in javafx using netbeans ide. want call method of class in other package class in package. both packages under same project. code of main class below :
package welcomepage; import javafx.application.application; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.combobox; import javafx.scene.control.label; import javafx.scene.control.passwordfield; import javafx.scene.control.textfield; import javafx.scene.control.tooltip; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.text.font; import javafx.scene.text.fontweight; import javafx.stage.stage; import help.*; public class welcomepage extends application { @override public void start(stage stage) { borderpane border = new borderpane(); border.settop(addvbox()); border.setleft(addvbox1()); scene scene = new scene(border,700,450); stage.setscene(scene); stage.setresizable(false); scene.getstylesheets().add (welcomepage.class.getresource("welcomepage.css").toexternalform()); stage.show(); } private vbox addvbox() { vbox vbox = new vbox(); vbox.setpadding(new insets(5, 12, 5, 20)); vbox.setspacing(10); // gap between nodes image image = new image(getclass().getresourceasstream("logo11.png")); label lb1=new label(" c - mark , attendance calculator"); lb1.setalignment(pos.center); lb1.setfont(font.font("calibri",fontweight.bold,28)); lb1.settextfill(color.black); lb1.setgraphic(new imageview(image)); vbox.getchildren().addall(lb1); return vbox; } private vbox addvbox1() { vbox vbox1=new vbox(); vbox1.setpadding(new insets(20, 2, 15, 20)); vbox1.setspacing(20); button btnl2=new button("sign in"); btnl2.setfont(font.font("calibri",fontweight.bold,16)); btnl2.setprefsize(300,60); btnl2.setstyle(" -fx-base: #0066cc;"); //image imageok = new image(getclass().getresourceasstream("icon22.png")); //btnl2.setgraphic(new imageview(imageok)); final tooltip tooltip2 = new tooltip(); tooltip2.settext("if have account,\nsign in here."); btnl2.settooltip(tooltip2); btnl2.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { signin(); } }); button btnl4=new button("help"); btnl4.setfont(font.font("calibri",fontweight.bold,16)); btnl4.setprefsize(300,60); btnl4.setstyle(" -fx-base: #0066cc;"); final tooltip tooltip4 = new tooltip(); tooltip4.settext("get content\nabout software."); btnl4.settooltip(tooltip4); btnl4.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { // want call method of other class here. } }); button btnl5=new button("about"); btnl5.setfont(font.font("calibri",fontweight.bold,16)); btnl5.setprefsize(300,60); btnl5.setstyle(" -fx-base: #0066cc;"); final tooltip tooltip5 = new tooltip(); tooltip5.settext("know about\nthis software."); btnl5.settooltip(tooltip5); btnl5.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { about(); } }); button btnl6=new button("exit"); btnl6.setfont(font.font("calibri",fontweight.bold,16)); btnl6.setprefsize(300,60); btnl6.setstyle(" -fx-base: #0066cc;"); final tooltip tooltip6 = new tooltip(); tooltip6.settext("exit if had\nfinished works."); btnl6.settooltip(tooltip6); btnl6.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { system.exit(0); } }); vbox1.getchildren().addall(btnl2,btnl4,btnl5,btnl6); return vbox1; } public void signin() { stage stage=new stage(); borderpane border = new borderpane(); border.settop(loginhbox1()); border.setleft(loginvbox1()); border.setright(loginvbox2()); scene scene = new scene(border,700,450); stage.setscene(scene); stage.setresizable(false); scene.getstylesheets().add (login.class.getresource("login.css").toexternalform()); stage.show(); } private hbox loginhbox1() { hbox hbox = new hbox(); hbox.setpadding(new insets(15, 12, 10, 180)); hbox.setspacing(10); label lb1=new label("log in or create new account"); lb1.setalignment(pos.center); lb1.setfont(font.font("calibri",fontweight.bold,26)); lb1.settextfill(color.black); hbox.getchildren().addall(lb1); return hbox; } private vbox loginvbox1() { vbox hbox = new vbox(); hbox.setpadding(new insets(20,30,15,50)); hbox.setspacing(10); label lb3=new label("log in"); lb3.setalignment(pos.center); lb3.setfont(font.font("calibri",fontweight.bold,24)); lb3.settextfill(color.black); label lb1=new label("username"); lb1.setalignment(pos.center); lb1.setfont(font.font("calibri",fontweight.bold,16)); lb1.settextfill(color.black); textfield t1=new textfield(); t1.setprefsize(150,30); label lb2=new label("password"); lb2.setalignment(pos.center); lb2.setfont(font.font("calibri",fontweight.bold,16)); lb2.settextfill(color.black); passwordfield pw1=new passwordfield(); pw1.setprefsize(150,30); button b1=new button("log in"); b1.setfont(font.font("calibri",fontweight.bold,16)); b1.setprefsize(80,5); hbox.getchildren().addall(lb3,lb1,t1,lb2,pw1,b1); return hbox; } private vbox loginvbox2() { vbox hbox1 = new vbox(); hbox1.setpadding(new insets(15, 50, 15, 10)); hbox1.setspacing(10); label lb4=new label("create new account"); lb4.setfont(font.font("calibri",fontweight.bold,24)); lb4.setprefsize(250,30); lb4.settextfill(color.black); label lb1=new label("full name "); lb1.setfont(font.font("calibri",fontweight.bold,18)); lb1.setprefsize(100, 30); lb1.settextfill(color.black); textfield t1=new textfield(); t1.setprefsize(50,30); label lb2=new label("user name "); lb2.setfont(font.font("calibri",fontweight.bold,18)); lb2.setprefsize(150, 30); lb2.settextfill(color.black); textfield t2=new textfield(); t2.setprefsize(100,30); label lb3=new label("password "); lb3.setfont(font.font("calibri",fontweight.bold,18)); lb3.setprefsize(150, 30); lb3.settextfill(color.black); passwordfield t3=new passwordfield(); t3.setprefsize(100,30); label lb5=new label("gender "); lb5.setfont(font.font("calibri",fontweight.bold,18)); lb5.setprefsize(150, 30); lb5.settextfill(color.black); observablelist<string> options2 = fxcollections.observablearraylist( "male","female"); final combobox combobox2 = new combobox(options2); combobox2.setprefsize(250,30); button btn1=new button("create"); btn1.setfont(font.font("calibri",fontweight.bold,18)); btn1.setprefsize(100,30); hbox1.getchildren().addall(lb4,lb1,t1,lb2,t2,lb3,t3,lb5,combobox2,btn1); return hbox1; } public void about() { stage stage=new stage(); borderpane border = new borderpane(); hbox hbox11 = abouthbox1(); border.settop(hbox11); border.setcenter(aboutvbox1()); border.setbottom(abouthbox2()); scene scene = new scene(border,700,450); stage.setscene(scene); stage.setresizable(false); scene.getstylesheets().add (about.class.getresource("about.css").toexternalform()); stage.show(); } private hbox abouthbox1() { hbox hbox11 = new hbox(); hbox11.setpadding(new insets(15, 12, 15, 320)); hbox11.setspacing(10); hbox11.setstyle("-fx-background-color: #336699;"); label lb1=new label("about"); lb1.setalignment(pos.center); lb1.setfont(font.font("trebuchet ms",fontweight.bold,20)); hbox11.getchildren().addall(lb1); return hbox11; } private vbox aboutvbox1() { vbox vbox11 = new vbox(); vbox11.setpadding(new insets(20)); vbox11.setspacing(5); label l1=new label("c - mark , attendance calculator"); l1.setfont(font.font("calibri",fontweight.bold,20)); l1.settextfill(color.green); label l2=new label("\nsoftware calculate c-mark , attendance easily.\n" + "supported in windows xp or above.\n" + "developed using java.\n" + "advantages : simple user interface, easy usage.\n\n" + "developed :\n" + "\t\t adarsh p.s \n" + "\t\t akhilnath a.r \n" + "\t\t arjun p das \n" + "\t\t tomin jacob "); l2.setfont(font.font("calibri",fontweight.bold,18)); l2.settextfill(color.green); vbox11.getchildren().addall(l1,l2); return vbox11; } private hbox abouthbox2() { hbox hbox12 = new hbox(); hbox12.setpadding(new insets(15, 12, 15, 300)); button btn1=new button("back"); btn1.setfont(font.font("calibri",fontweight.bold,18)); btn1.setprefsize(100,40); btn1.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { } }); hbox12.getchildren().addall(btn1); return hbox12; } public static void main(string[] args) { launch(args); } }
i commented place want call other class. following code of class want worked when button pressed:
package help; import javafx.application.application; import static javafx.application.application.launch; import javafx.geometry.insets; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.text.font; import javafx.scene.text.fontweight; import javafx.stage.stage; public class extends application { @override public void start(stage stage) { // use border pane root scene borderpane border = new borderpane(); hbox hbox = helphbox1(); border.settop(hbox); border.setcenter(helpvbox1()); border.setbottom(helphbox2()); scene scene = new scene(border,700,450); stage.setscene(scene); stage.setresizable(false); scene.getstylesheets().add (help.class.getresource("help.css").toexternalform()); stage.show(); } private hbox helphbox1() { hbox hbox = new hbox(); hbox.setpadding(new insets(15, 12, 15, 300)); hbox.setspacing(10); // gap between nodes hbox.setstyle("-fx-background-color: #336699;"); label lb1=new label("help"); lb1.setalignment(pos.center); lb1.setfont(font.font("trebuchet ms",fontweight.bold,20)); hbox.getchildren().addall(lb1); return hbox; } private vbox helpvbox1() { vbox vbox = new vbox(); vbox.setpadding(new insets(20)); // set sides 10 vbox.setspacing(5); // gap between nodes label l1=new label(""); l1.setfont(font.font("calibri",fontweight.bold,20)); l1.settextfill(color.green); vbox.getchildren().addall(l1); return vbox; } private hbox helphbox2() { hbox hbox1 = new hbox(); hbox1.setpadding(new insets(15, 12, 15, 300)); button btn1=new button("back"); btn1.setfont(font.font("calibri",fontweight.bold,18)); btn1.setprefsize(100,40); hbox1.getchildren().addall(btn1); return hbox1; } /** * main() method ignored in correctly deployed javafx application. * main() serves fallback in case application can not * launched through deployment artifacts, e.g., in ides limited fx * support. netbeans ignores main(). * * @param args command line arguments */ public static void main(string[] args) { launch(args); } }
the below solution based on ur question 'now want call method of class in other package class in package.'not according code
class -1
package abc; class { public void demo() { ////////code } }
class -2
package def; import abc.a; class b { public static void main(string arg[]) { obj = new a(); obj.demo(); // class method called class b in different package. } }
Comments
Post a Comment