swing - Java access frame -
im trying following code using java.swing , java.awt
init.java
package es.com.gui; import java.awt.*; import javax.swing.*; public class init extends jframe { public init() { // start actual gui settitle("basic calculator"); setsize(300, 300); setlocationrelativeto(null); setlayout(new flowlayout()); setdefaultcloseoperation(exit_on_close); // add gui elements constructor constructor = new constructor(); jtextfield input1 = constructor.addtextfield("e"); input1.setcolumns(25); } }
constructor.java
package es.com.gui; import javax.swing.*; import java.awt.*; public class constructor extends jframe { public jlabel addlabel(string text) { jlabel label = new jlabel(text); add(label); return label; } public jbutton addbutton(string text) { jbutton button = new jbutton(text); add(button); return button; } public jtextfield addtextfield(string text) { jtextfield textarea = new jtextfield(text); add(textarea); return textarea; } }
the thing want have functions construct new gui elements on class , heres problem if add functions init.java works fine adding them constructor.java seems not work (no error @ all, elements never appear)
your question specific example of general question,
how can call methods of 1 object within object.
and 1 possible solution pass references. example change constructor's constructor accept init object , use initialize init field, init:
public class constructor { private init init; public constructor(init init) { this.init = init; } // can use init init instance throughout class
and pass this
in when calling constructor.
as aside: please read , follow java naming conventions including giving classes names start upper case letters (see code above example).
Comments
Post a Comment