java - JPanel size not being set -
i'm working on irc bot , today create gui it.
i'm trying create column layout has 2 parts, left , right. left side show console output, right contain controls (joining channels, commands etc).
i'm having issues creating 2 columns. have jpanel whole width , height of window , has border of 10 pixels, , have 2 panels within that; left , right.
the left , right panels reason taking whole size of window, , right panel overlapping everything.
here's example picture: http://i.imgur.com/lc4vhvh.png white right panel, should half size , have identical black panel on left of it.
here's current code, sorry if it's messy, new whole swing gui.. help.
package tk.taylerking.gribbybot; import java.awt.color; import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.util.arraylist; import java.util.hashmap; import javax.swing.borderfactory; import javax.swing.swingutilities; import javax.swing.uimanager; import com.alee.laf.weblookandfeel; import com.alee.laf.menu.webmenubar; import com.alee.laf.menu.webmenuitem; import com.alee.laf.panel.webpanel; import com.alee.laf.rootpane.webframe; public class gribbybot extends webframe { private static final long serialversionuid = 4641597667372956773l; public static hashmap<string, arraylist<string>> connections = new hashmap<string, arraylist<string>>(); public static void main(string[] args) throws exception { uimanager.setlookandfeel(weblookandfeel.class.getcanonicalname()); swingutilities.invokelater(new runnable(){ public void run(){ gribbybot gb = new gribbybot(); gb.setvisible(true); } }); } public gribbybot(){ webpanel panel = new webpanel(); panel.setborder(borderfactory.createemptyborder(10, 10, 10, 10)); panel.setpreferredsize(new dimension(780, 580)); webmenubar menu = new webmenubar(); webmenuitem file = new webmenuitem("exit"); file.setmnemonic(keyevent.vk_e); file.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { system.exit(0); } }); menu.add(file); setjmenubar(menu); webpanel left = new webpanel(); left.setpreferredsize(new dimension(380, 580)); left.setbackground(color.black); webpanel right = new webpanel(); right.setpreferredsize(new dimension(380, 580)); right.setbackground(color.white); add(panel); panel.add(left); panel.add(right); settitle("gribbybot"); setsize(800, 600); setlocationrelativeto(null); setdefaultcloseoperation(exit_on_close); setresizable(false); } }
on side note, variables prefixed "web" same swing, it's custom gui.
override jcomponent#getpreferredsize() instead of using setpreferredsize()
read more should avoid use of set(preferred|maximum|minimum)size methods in java swing?
if extremely needed in case of performing custom painting try in way:
sample code:
final jpanel panel = new jpanel(){ @override public void paintcomponent(graphics g){ super.paintcomponent(g); // custom painting code here } @override public dimension getpreferredsize() { return new dimension(40, 40); } };
why using setpreferredsize()
method whereas can achieve design using proper layout such boxlayout
, gridlayout
, borderlayout
etc.
read more layout how use various layout managers
edit
try jpanel panel = new jpanel(new gridlayout(1,2));
Comments
Post a Comment