swing - Java setResizable issue -
package data; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; public class main extends jframe implements runnable { private jpanel contentpane; private jpanel pp = new jpanel(); thread page = new thread(); public void run() { while (!thread.interrupted()) { } } public main() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 640 + 16, 480 + 39); contentpane = new jpanel(); contentpane.setbounds(0, 0, 640, 480); contentpane.setlayout(null); setcontentpane(contentpane); pp.setbounds(0, 0, 640, 480); pp.setbackground(color.black); contentpane.add(pp); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { main frame = new main(); frame.setvisible(true); frame.setresizable(false); } catch (exception e) { e.printstacktrace(); } } }); } }
the above code works setresizable causes issue: http://i.stack.imgur.com/hqxpu.png
if remove setresizable grey @ bottom , right black it's meant to. how can disable resizing without causing issue?
you're using absolute layout (no layoutmanager
set), , black panel has fixed bounds. , that's reason black panel won't fill parent's bounds when parent resized.
solution: use layoutmanager
automatically recalculates bounds of content fills available space.
// borderlayout friend contentpane.setlayout(new borderlayout()); ... // delete line, no need set fixed bounds // pp.setbounds(0, 0, 640, 480);
more on how use layout managers in awt/swing:
the java™ tutorials - using layout managers
layout managers have 2 purposes:
- calculate min/max/preferred size of container
- layout components setting bounds within container.
if want black panel have size of 640x480, , window non-resizable, can set preferred size , pack window, causing size become appropriate content's preferred dimensions:
pp.setpreferredsize(new dimension(640, 480)); ... pack();
Comments
Post a Comment