java - How can i add two class that extends JPanel in One JFrame -
i created 2 class extends jpanel , want add 2 class in 1 frame. unable it. please.
my classes -->
import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class calbutton extends jpanel { private jbutton[] buttons; private static final string[] buttonnames = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" }; private jpanel buttonpanel; public calbutton() { // todo auto-generated constructor stub buttons = new jbutton[buttonnames.length]; buttonpanel = new jpanel(); buttonpanel.setlayout(new gridlayout(4, 4, 3, 3)); for(int i=0; i<buttonnames.length; i++){ buttons[i] = new jbutton(buttonnames[i]); buttonpanel.add(buttons[i]); } } }
another class-->
import java.awt.gridlayout; import javax.swing.jpanel; import javax.swing.jtextfield; public class calfield extends jpanel { private jpanel panelfield; private jtextfield field; public calfield() { // todo auto-generated constructor stub panelfield = new jpanel(); panelfield.setlayout(new gridlayout(1, 1,5,5)); field = new jtextfield(20); panelfield.add(field); } }
main class-->
public class calculator { public static void main(string[] args) { jframe application = new jframe("calculator"); calfield calfield = new calfield(); calbutton calbutton = new calbutton(); application.setlayout(new gridlayout(2, 1)); application.setdefaultcloseoperation(jframe.exit_on_close); application.setsize(400, 450); application.setvisible(true); } }
anyone kindly solve problem please. i'm stuck problem.
public calbutton() { // todo auto-generated constructor stub buttons = new jbutton[buttonnames.length]; buttonpanel = new jpanel(); buttonpanel.setlayout(new gridlayout(4, 4, 3, 3)); for(int i=0; i<buttonnames.length; i++){ buttons[i] = new jbutton(buttonnames[i]); buttonpanel.add(buttons[i]); } }
the calbutton
class "is a" jpanel because extend jpanel there no need create jpanel. add buttons class:
public calbutton() { // todo auto-generated constructor stub buttons = new jbutton[buttonnames.length]; //buttonpanel = new jpanel(); //buttonpanel.setlayout(new gridlayout(4, 4, 3, 3)); setlayout(new gridlayout(4, 4, 3, 3)); for(int i=0; i<buttonnames.length; i++){ buttons[i] = new jbutton(buttonnames[i]); //buttonpanel.add(buttons[i]); add(buttons[i]); } }
same calcfield
class except in case don't need set layout manager. can use default flowlayout.
then second problem never add these panels frame:
calfield calfield = new calfield(); calbutton calbutton = new calbutton(); //application.setlayout(new gridlayout(2, 1)); application.add(calfield, borderlayout.page_start); application.add(calbutton, borderlayout.center);
Comments
Post a Comment