java - How expand a jframe in left side? -
i have 3 jpanel
s. left , right panels hidden. center panel visible default.
when press 1 button, frame width increased right panel's width , right panel becomes visible.
my problem left panel because frame cannot increased in left direction.
my solution is:
public void mousepressed(mouseevent e) { int framewidth = frame.getbounds().width; int frameheight = frame.getbounds().height; int panelwidth = leftpanel.getpreferredsize().width; point currcoords = frame.getlocationonscreen(); if(!_leftpanelopened) { frame.setsize(new dimension(framewidth + panelwidth, frameheight)); leftpanel.setvisible(true); frame.setlocation(currcoords.x - panelwidth, currcoords.y); _leftpanelopened = true; } else { leftpanel.setvisible(false); frame.setsize(new dimension(framewidth - panelwidth, frameheight)); frame.setlocation(currcoords.x + panelwidth, currcoords.y); _leftpanelopened = false; } }
it works, frame shortly blinks. can avoid short blink?
edit:
public class main { private static jpanel leftoption = new jpanel(); private static jpanel rightoption = new jpanel(); private static jframe frame = new jframe(); private static jpanel leftpanel = new jpanel(); private static jpanel rightpanel = new jpanel(); private static jpanel _centerpanel = new jpanel(); private static boolean _leftpanelopened = false; private static boolean _rightpanelopened = false; public static void main(string[] args) { leftoption.setpreferredsize(new dimension(50, 40)); leftoption.setbackground(color.cyan); leftoption.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { int framewidth = frame.getbounds().width; int frameheight = frame.getbounds().height; int panelwidth = leftpanel.getpreferredsize().width; point currcoords = frame.getlocationonscreen(); if(!_leftpanelopened) { frame.setbounds(currcoords.x - panelwidth, currcoords.y, framewidth + panelwidth, frameheight); leftpanel.setvisible(true); _leftpanelopened = true; } else { leftpanel.setvisible(false); frame.setbounds(currcoords.x + panelwidth, currcoords.y, framewidth - panelwidth, frameheight); _leftpanelopened = false; } } @override public void mousereleased(mouseevent e) { } }); rightoption.setpreferredsize(new dimension(50, 40)); rightoption.setbackground(color.orange); rightoption.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { int framewidth = frame.getbounds().width; int frameheight = frame.getbounds().height; int panelwidth = rightpanel.getpreferredsize().width; if(!_rightpanelopened) { frame.setsize(new dimension(framewidth+panelwidth, frameheight)); rightpanel.setvisible(true); _rightpanelopened = true; } else { rightpanel.setvisible(false); frame.setsize(new dimension(framewidth-panelwidth, frameheight)); _rightpanelopened = false; } } @override public void mousereleased(mouseevent e) { } }); _centerpanel.setpreferredsize(new dimension(300, 600)); _centerpanel.setbackground(color.white); _centerpanel.add(leftoption); _centerpanel.add(rightoption); leftpanel.setpreferredsize(new dimension(300, 600)); leftpanel.setbackground(color.blue); leftpanel.setvisible(false); rightpanel.setpreferredsize(new dimension(300, 600)); rightpanel.setbackground(color.red); rightpanel.setvisible(false); frame.add(leftpanel, borderlayout.line_start); frame.add(_centerpanel, borderlayout.center); frame.add(rightpanel, borderlayout.line_end); frame.setsize(new dimension(300, 600)); frame.getcontentpane().setbackground(color.white); frame.setvisible(true); } }
you can call setbounds()
, allows set position , size @ same time. actually, under hood, both setsize()
, setlocation()
call setbounds()
in end.
furthermore, can avoid calling setvisible(true)
before panel in place.
therefore, how changing method to:
if(!_leftpanelopened) { frame.setbounds(currcoords.x - panelwidth, currcoords.yframewidth + panelwidth, frameheight); leftpanel.setvisible(true); _leftpanelopened = true; }
Comments
Post a Comment