java - Rescale JFrame content -
i creating jframe
, , when window expanded want content stay centered, instead of staying same distance away side. using windowbuilder on eclipse.
is there quick way this?
one way, have container use gridbaglayout , add single content container without constraints.
for example:
import java.awt.gridbaglayout; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; import javax.swing.*; public class centeredcontent extends jpanel { public static final string img_path = "https://duke.kenai.com/iconsized/duke.gif"; public centeredcontent() throws ioexception { url imgurl = new url(img_path); bufferedimage img = imageio.read(imgurl); icon imgicon = new imageicon(img); jlabel label = new jlabel(imgicon); setlayout(new gridbaglayout()); add(label); } private static void createandshowgui() { centeredcontent mainpanel = null; try { mainpanel = new centeredcontent(); } catch (ioexception e) { e.printstacktrace(); system.exit(-1); } jframe frame = new jframe("centeredcontent"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment