i using grid bag layout java application, problem is, not placing components in page start. here code using:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class trial extends jframe { jlabel banner; container c; gridbagconstraints gbc = new gridbagconstraints(); gridbaglayout gbl; public trial() { settitle("attendence manager"); seticonimage(toolkit.getdefaulttoolkit().getimage("images/icon.png")); dimension dim= toolkit.getdefaulttoolkit().getscreensize(); setsize(new dimension(dim.width-20,dim.height-100)); c= getcontentpane(); gbl= new gridbaglayout(); setlayout(gbl); banner = new jlabel(new imageicon("images/banner.jpg")); gbc.anchor=gridbagconstraints.page_start; gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=gridbagconstraints.remainder; c.add(banner,gbc); this.setvisible(true); addwindowlistener(new mywindowadapter()); } public static void main(string[] args) { trial t = new trial(); } } class mywindowadapter extends windowadapter { //loginpage sp; public mywindowadapter() { } @override public void windowclosing(windowevent we) { system.exit(0); } }
i have tried
gbc.anchor = gridbagconstraints.first_line_start;
even didn't work. output getting:
first need set
gbc.fill = gridbagconstraints.both; gbc.weightx = 1.0; gbc.weighty = 1.0;
but make jlabel fill whole space not icon inside jlabel. if want image scale , use whole space. suggest read image bufferedimage
, override paintcomponent()
method draw scaled instance of bufferedimage. this:
public trail() { settitle("attendence manager"); seticonimage(toolkit.getdefaulttoolkit().getimage("images/icon.png")); dimension dim = toolkit.getdefaulttoolkit().getscreensize(); setsize(new dimension(dim.width - 20, dim.height - 100)); c = getcontentpane(); gbl = new gridbaglayout(); setlayout(gbl); try { final bufferedimage image = imageio.read(new file("images/sample.jpg")); banner = new jlabel(){ public void paintcomponent(graphics g) { g.drawimage(image, 0, 0, getwidth(), getheight(), null); } }; gbc.fill = gridbagconstraints.both; gbc.weightx = 1.0; gbc.weighty = 1.0; c.add(banner, gbc); } catch (ioexception ex) { logger.getlogger(trail.class.getname()).log(level.severe, null, ex); } this.setvisible(true); addwindowlistener(new mywindowadapter()); }
Comments
Post a Comment