java - How to add several panels with labels to a single frame? -
i learning java swings, , creating 1 frame , want add more 1 panel different orientations, see below in code jpanet_1
, jpanel_2
each of them has specific dimensions set using setbound()
method.
the problem @ run time, "hello world" appears in second panel , not appear in first one. tried switch order in adding 2 panels main frame follows:
jframe_2.add(jpanel_2); jframe_2.add(jpanel_1);
but then, "hello world" added panel_2 only.
please let me know how add 2 panels frame the statement "hello world" appears in both
as see in code, specifying dimensions each panel wish add frame, add it, there other recommended way add panels frames?
code:
public class gui_01 { jframe jframe_1; jframe jframe_2; jpanel jpanel_1; jpanel jpanel_2; final jlabel jlabel_hello = new jlabel("hello world"); joptionpane joptions; final string[] options = {"yes", "no", "maybe"}; public gui_01() { // todo auto-generated constructor stub setupgui1(); setupgui2(); } private void setupgui2() { // todo auto-generated method stub jframe_2 = new jframe("border demo"); jpanel_1 = new jpanel(); jpanel_2 = new jpanel(); jpanel_1.setborder(borderfactory.createtitledborder("title")); jpanel_1.setbounds(30, 100, 110, 300); jpanel_1.add(jlabel_hello); jpanel_2.setborder(borderfactory.createloweredbevelborder()); jpanel_2.setbounds(20, 50, 120, 80); jpanel_2.add(jlabel_hello); jframe_2.setbounds(0, 0, 600, 600); jframe_2.add(jpanel_1); jframe_2.add(jpanel_2); jframe_2.setvisible(true); }
the problem @ run time, "hello world" appears in second panel , not appear in
that correct. component can have single parent.
if want text "hello world" need create 2 jlabels , add 1 of labels each panel.
jlabel label1 = new jlabel("hello world"); jpanel panel1 = new jpanel(); panel1.add( label1 ); jlabel label2 = new jlabel("hello world"); jpanel panel2 = new jpanel(); panel2.add( label2 );
i tried use gridlayout not place jpanel in specific cell of gridlayout..
you can't add components random cells. must have components in every cell, or in case of gridbaglayout, component can span multiple cells.
Comments
Post a Comment