java - Two of my panels connect together when adding to the third panel -
i trying add 2 panels in each has own components onto panel, when program executes 2 panels amalgamate each other. please check out code.
import javax.swing.*; import java.awt.*; /* * 2 labels withdraw/deposit. * 2 textfields entering withdrawal/deposit amount. * 1 label balance, 1 non-editable textfield balance. * event listener on calculate button. * action performed: balance minors withdrawal plus deposit */ / ** * class manages input numbers. user input withdrawal/deposit amount * , program display balacne after transfers. * @author administrator * */ public class datapanel extends jpanel { public final int textfield_length = 10; public final int layout_vgap = 25; public final int layout_hgap = 15; /** * constructor sets layout, creates labels/fields. */ public jpanel pane1 = new jpanel(); public jpanel pane2 = new jpanel(); public jpanel pane = new jpanel(new gridlayout(1,2)); //create necessary fields , labels jlabel withdrawallabel = new jlabel("withdrawal: "); jlabel depositlabel = new jlabel("deposit: "); jtextfield withdrawaltext = new jtextfield(textfield_length); jtextfield deposittext = new jtextfield(textfield_length); jlabel balancelabel = new jlabel("balance: "); jtextfield balancetextfield = new jtextfield(textfield_length); jlabel totalsavinglabel = new jlabel("recently saved: "); jtextfield totalsavingtextfield = new jtextfield(textfield_length); jlabel totalspentlabel = new jlabel("recently spent: "); jtextfield totalspenttextfield = new jtextfield(textfield_length); //set textfields non editable. //set layout, 3 rows + 2 columns. public datapanel() { setlayout(new gridlayout(2,1)); buildpane1(); buildpane2(); buildpane(); add(pane); //add components panel datapanel. } public void buildpane() { pane.add(pane1); pane.add(pane2); } public void buildpane1() { setlayout(new gridlayout(3,2)); setborder(borderfactory.createlineborder(color.pink, 2, true)); balancetextfield.seteditable(false); pane1.add(withdrawallabel); pane1.add(withdrawaltext); pane1.add(depositlabel); pane1.add(deposittext); pane1.add(balancelabel); pane1.add(balancetextfield); } public void buildpane2() { setlayout(new gridlayout(2,2)); setborder(borderfactory.createlineborder(color.black, 2, true)); totalsavingtextfield.seteditable(false); totalspenttextfield.seteditable(false); pane2.add(totalsavinglabel); pane2.add(totalsavingtextfield); pane2.add(totalspentlabel); pane2.add(totalspenttextfield); } }
jframe class:
import java.awt.borderlayout; import java.awt.gridlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtabbedpane; public class mainframe extends jframe { public final int width = 1000; public final int height = 800; public datapanel datapane; public infopanel infopane; public menu menu; public jtabbedpane tabbedpane = new jtabbedpane(); //tab #1 public jpanel spendingpane; //tab #2 public jpanel personalpane; //tab #3 public jpanel socialpane; //tab #4 public jpanel chatpane; /** * constructor. */ public mainframe() { settitle("personal banking"); setsize(width, height); setlayout(new borderlayout()); buildmenu(); buildpanel(); add(tabbedpane); pack(); setvisible(true); } /** * methods builds components. */ private void buildpanel() { //build main panel (spending panel). datapane = new datapanel(); //add numbers panel tabbed panel. tabbedpane.addtab("numbers", datapane); //build personal information panel. infopane = new infopanel(); personalpane = new jpanel(); personalpane.add(infopane); //add personal information panel tabbed panel. tabbedpane.addtab("personal", personalpane); } /** * function builds menu bar , menu items. */ private void buildmenu() { menu = new menu(); setjmenubar(menu.mainmenubar); } public static void main(string args[]) { new mainframe(); } }
i have tried several methods, didn't work.
***well apparently pasted first class twice. fixed it. sorry that.
based on example (and little change background color of each pane), can see been added
however, this...
public void buildpane1() { setlayout(new gridlayout(3,2)); setborder(borderfactory.createlineborder(color.pink, 2, true));
and this...
public void buildpane2() { setlayout(new gridlayout(2,2)); setborder(borderfactory.createlineborder(color.black, 2, true));
look suspicious, been applied datapanel
, not (as suspect want) individual panels...
so changing them more like...
public void buildpane1() { pane1.setlayout(new gridlayout(3, 2)); pane1.setborder(borderfactory.createlineborder(color.pink, 2, true)); //... public void buildpane2() { pane2.setlayout(new gridlayout(2, 2)); pane2.setborder(borderfactory.createlineborder(color.black, 2, true));
gives me...
Comments
Post a Comment