swing - Java inheritance or GUI gone wrong -
despite tips, i'm still getting 1 wrong. end 1 basic window , 1 features, without basic ones previous window. instead, 1 new window combining basic , new features. here code i've got: (also approach advise?)
package windows; import java.awt.*; import javax.swing.*; public abstract class windowtemplate extends jframe { /** * create gui , show it. thread safety, method should * invoked event-dispatching thread. */ public windowtemplate () { jframe myframe = new jframe("my first window"); myframe.setdefaultcloseoperation(jframe.exit_on_close); myframe.setvisible(true); myframe.setsize(550, 450); myframe.setlocationrelativeto(null); // jlabel emptylabel = new jlabel(""); // emptylabel.setpreferredsize(new dimension(550, 450)); // myframe.getcontentpane().setlayout(new cardlayout()); // myframe.getcontentpane().add(emptylabel, borderlayout.center); // myframe.pack(); } }
now 1 meant "extended":
package windows; import java.awt.*; import javax.swing.*; public class a_welcome extends windowtemplate { public a_welcome() { jpanel area = new jpanel(); jlabel text = new jlabel("one line line , line"); // , jlabel.center); // text.setbounds(80, 400, 400, 50); add(area); // area.setlayout(null); area.add(text, new cardlayout()); // area.add(text); // , borderlayout.center); font font = new font("sansserif", font.bold, 30); text.setfont(font); text.setforeground(color.green); area.setbackground(color.darkgray); area.setsize(550, 450); } } // timer-after 5 seconds-go next window (countdown in bottom right corner)
and main:
package windows; public class launcher { public static void main(string[] args) { // schedule job event-dispatching thread: // creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { // windowtemplate.createwindow(); // a_welcome.createwindow(); a_welcome window = new a_welcome(); window.setvisible(true); } }); } }
-- alternatively --
public class windowtemplate extends jframe { // constructor public windowtemplate() { init(); } public void init() { // add basic components jframe myframe = new jframe("my first window"); myframe.setdefaultcloseoperation(jframe.exit_on_close); myframe.setvisible(true); myframe.setsize(550, 450); myframe.setlocationrelativeto(null); } }
and
public class a_welcome extends windowtemplate { public a_welcome() { super(); } @override public void init() { super.init(); // important base stuff // add other components jpanel area = new jpanel(); jlabel text = new jlabel("one line line , line"); add(area); area.add(text, new cardlayout()); font font = new font("sansserif", font.bold, 30); text.setfont(font); text.setforeground(color.green); area.setbackground(color.darkgray); area.setsize(550, 450); } }
sorry lots of code , help!
i'm not quite sure problem is, windowtemplate
is jframe
, don't want create new jframe in constructor rather "apply" method this
instead of myframe
. try this:
public windowtemplate() { super("my first window"); this.setdefaultcloseoperation(jframe.exit_on_close); // "this" optional setvisible(true); setsize(550, 450); setlocationrelativeto(null); }
and
public a_welcome() { super(); // implicit, because a_welcome extends windowtemplate, has got constructor without parameters //[add more stuff here] }
when you're creating new a_welcome
, constructor call super constructor, windowtemplate
in turn call jframe
constructor
Comments
Post a Comment