java - Align panels with backgrounds correctly? -


i started java guis few weeks ago , have difficulty alignment. basically, i'm trying have 2 panels different background image (top bar , content) , want align them 1 after another. problem is, can't use borderlayout.north , borderlayout.south, because background image loses original size , gets tiny.

how can align them correctly, without losing original size?

here's code:

package main;  import java.awt.borderlayout; import java.awt.graphics; import java.awt.image;  import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jpanel;  public class imagetest {    public static void main(string[] args) {     imageframe frame = new imageframe("topbar.png", "contentimage.png");     frame.setsize(640,480);     frame.setvisible(true);   } }  class imagepanel extends jpanel {  private image img;  public imagepanel(string img) {     this(new imageicon(img).getimage());     }  public imagepanel(image img) {     this.img = img; }  public void paintcomponent(graphics g) {     g.drawimage(img, 0, 0, null); }  }  class imageframe extends jframe { public imageframe(string topbar, string body) {     setlayout(new borderlayout());     imagepanel toppanel = new imagepanel(topbar);     imagepanel bodypanel = new imagepanel(body);     add(toppanel, borderlayout.north);     add(bodypanel, borderlayout.south);     pack(); } } 

there number of issues popup out @ me

  1. you're not calling super.paintcomponent. important , can not understated
  2. you should using imageio load images. ala fact supports wider range of image formats, loads images concurrent , throws useful exceptions when goes wrong
  3. you're not supplying preferredsize values. used layout manager decide how best layout component. remember though, hints , layout managers within there rights ignore them

check out reading/loading image more details on imageio


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -