java - Adding a panel from a method to a frame -
i didn't know how else phrase essentially:
-i have few separate "pieces" trying add onto master frame; keep code getting unwieldy have each "piece" own class.
-i'm getting stuck on adding panells onto master frame, because classes aren't panels, rather method of class creates panel, creates issues don't know how solve.
piece (works on own when have make dialog instead of panel):
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class piecething3 extends jpanel //<switched jdialog { //set variables here private actionlistener pieceaction = new actionlistener() { public void actionperformed (actionevent ae) { // action listener (this works) } }; private void createpiece() { //setdefaultcloseoperation(windowconstants.dispose_on_close); //setlocationbyplatform(true); // above commented out when switch dialog panel jpanel contentpane = new jpanel(); //something uses pieceaction here //two buttons, b , s, action listeners here contentpane.add(b); contentpane.add(s); add(contentpane); //pack(); //again, commented out switch dialog setvisible(true); system.out.println("hi i'm done"); //just check , make sure it's done } public static void main(string[] args) { // todo auto-generated method stub swingutilities.invokelater(new runnable() { public void run() { new piecething3().createpiece(); } }); } }
sorry vague, intricacies not important general idea - works when have create own dialog box, trying make panel within master code, below:
master:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class collectgui extends jframe{ private void createdialog(){ this.setsize(2000,1000); this.setlocation(0,0); this.settitle("title"); piecething3 pt = new piecething3(); //here, if pt.main(null); while in "dialog mode" (rather panel) pops dialog box , hunky dory. don't know how add method panel. this.add(pt.main(null)); //this gives error this.setvisible(true); } public static void main(string[] args) { // todo auto-generated method stub new collectgui().createdialog(); } }
as said in comments, if pt.main(null) when pt set make dialog, it, if try add pt.main(null) panel throws error. can give me insight on how add method of class rather class? i'm pretty stumped.
thank you!!
you on right track working maintain separation of concerns , implement gui in number of distinct components. try this:
panel1
import javax.swing.jlabel; import javax.swing.jpanel; public class panel1 extends jpanel { public panel1() { this.add(new jlabel("this panel 1")); } }
panel2
import javax.swing.jlabel; import javax.swing.jpanel; public class panel2 extends jpanel { public panel2() { this.add(new jlabel("this panel 2")); } }
jframe
import java.awt.borderlayout; import javax.swing.jframe; import org.yaorma.example.jframe.panel.panel1.panel1; import org.yaorma.example.jframe.panel.panel2.panel2; public class examplejframe extends jframe { public examplejframe() { super("example jframe application"); this.setdefaultcloseoperation(jframe.exit_on_close); this.setsize(400,400); this.setlayout(new borderlayout()); panel1 pan1 = new panel1(); panel2 pan2 = new panel2(); this.add(pan1, borderlayout.north); this.add(pan2, borderlayout.south); this.setvisible(true); } }
main:
public class exampleapplication { public static void main(string[] args) throws exception { new examplejframe(); } }
edit: here's panel1 little more content.
import java.awt.borderlayout; import java.awt.container; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import org.yaorma.example.action.sayhello.sayhelloaction; public class panel1 extends jpanel { // // instance variables // private jbutton pressmebutton; // // constructor // public panel1() { this.setlayout(new borderlayout()); this.add(new jlabel("this panel 1"), borderlayout.north); this.initpressmebutton(); } // // button // private void initpressmebutton() { this.pressmebutton = new jbutton("press me"); this.pressmebutton.addactionlistener(new pressmebuttonactionlistener()); this.add(pressmebutton, borderlayout.south); } // // method parent jframe // private jframe getparentjframe() { container con = this; while(con != null) { con = con.getparent(); if(con instanceof jframe) { return (jframe)con; } } return null; } // // action listener press me button // private class pressmebuttonactionlistener implements actionlistener { @override public void actionperformed(actionevent e) { jframe jframe = getparentjframe(); sayhelloaction action = new sayhelloaction(jframe); action.execute(); } } }
action called button:
import javax.swing.jframe; import javax.swing.joptionpane; public class sayhelloaction { private jframe owner; public sayhelloaction(jframe owner) { this.owner = owner; } public void execute() { joptionpane.showmessagedialog(owner, "hello world"); } }
Comments
Post a Comment