Java Swing Image doesn't show -



Java Swing Image doesn't show -

it's first post here , english language isn't pretty hope guy's understand problem have , hope nil wrong here.

my problem:

i'm learning atm swing , how works, have problems image doesnt show up. maybe dont understand part of swing hope can explain me why image doesnt loading can larn , improve work : )

i tried much variatons failed , dont know why. tried graphics.

my program:

jframe -> jpanel -> jlabel (which have image , should set on jpanel or maybe there direct way on jpanel)

test2.jpg in bundle folder , eclipse dont shout error.

also jpanel in separate class , dont extend jframe gui class.

here 3 classes:

start:

package verwaltungssoftware; public class start { //start der applikation public static void main(string[] args) { system.out.println("willkommen bei der verwaltungssoftware fuer die jobsuche"); new gui(); } }

gui:

package verwaltungssoftware; import java.awt.borderlayout; import java.awt.dimension; import java.awt.toolkit; import javax.swing.jframe; public class gui { //importiert auflösung des bildschirms toolkit tk = toolkit.getdefaulttoolkit(); dimension screensize = tk.getscreensize(); //setzt variablen für die auflösung public int aufloesungbreite = screensize.width; public int aufloesunghoehe = screensize.height; //setzt die berechnung des jframe hauptfenster location private int breite = aufloesungbreite/2 - 640; private int hoehe = aufloesunghoehe/2 - 400; public gui() { jframe hauptfenster = new jframe("verwaltungssoftware fuer die jobsuche"); hauptfenster.setdefaultcloseoperation(hauptfenster.exit_on_close); hauptfenster.setresizable(false); hauptfenster.setlocation(breite, hoehe); hauptfenster.setsize(1280,800); hauptfenster.setlayout(new borderlayout(5,5)); //addet hauptpanel zum jframe panel hauptpanel = new panel(); hauptfenster.add(hauptpanel); hauptpanel.setvisible(true); hauptfenster.setvisible(true); } }

and panel:

package verwaltungssoftware; import java.awt.borderlayout; import java.awt.image; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jlabel; import javax.swing.jpanel; public class panel extends jpanel { private static final long serialversionuid = 6769810448979262470l; //variablen image icon1; //konstruktor public panel() { seek { icon1 = imageio.read(getclass().getresource("test2.jpg")); } grab (ioexception e) { e.printstacktrace(); } jpanel panelhauptfenster = new jpanel(); panelhauptfenster.setlayout(new borderlayout (5,5)); panelhauptfenster.setsize(1280,800); panelhauptfenster.setlocation(0,0); panelhauptfenster.setvisible(true); jlabel mylabel=new jlabel(); mylabel.setlocation(0,0); mylabel.setsize(panelhauptfenster.getwidth(),panelhauptfenster.getheight()); mylabel.seticon(new imageicon(icon1)); mylabel.setvisible(true); panelhauptfenster.add(mylabel); } }

thank much in advance help.

the source below works. changes include:

returning sensible preferred size. removing calls setsize(..). factoring out panel image added, , instead adding straight panel instance. but panel renamed imagepanel not same name existing awt class! removing calls setvisible(..). thing applies top level containers jframe or jdialog. rest, add together them container made visible. import java.awt.*; import javax.swing.*; import java.net.url; import javax.imageio.imageio; class gui { //importiert auflösung des bildschirms toolkit tk = toolkit.getdefaulttoolkit(); dimension screensize = tk.getscreensize(); //setzt variablen für die auflösung public int aufloesungbreite = screensize.width; public int aufloesunghoehe = screensize.height; //setzt die berechnung des jframe hauptfenster location private int breite = aufloesungbreite/2 - 640; private int hoehe = aufloesunghoehe/2 - 400; public gui() { jframe hauptfenster = new jframe("verwaltungssoftware fuer die jobsuche"); hauptfenster.setdefaultcloseoperation(hauptfenster.exit_on_close); hauptfenster.setresizable(false); hauptfenster.setlocation(breite, hoehe); hauptfenster.setsize(1280,800); hauptfenster.setlayout(new borderlayout(5,5)); //addet hauptpanel zum jframe imagepanel hauptpanel = new imagepanel(); hauptfenster.add(hauptpanel); hauptpanel.setvisible(true); hauptfenster.setvisible(true); } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { new gui(); } }; swingutilities.invokelater(r); } } class imagepanel extends jpanel { //variablen image icon1; //konstruktor public imagepanel() { seek { url url = new url("http://i.stack.imgur.com/7bi1y.jpg"); icon1 = imageio.read(url); } grab (exception e) { e.printstacktrace(); } setlayout(new borderlayout (5,5)); jlabel mylabel=new jlabel(new imageicon(icon1)); add(mylabel); } // important! @override public dimension getpreferredsize() { homecoming new dimension(icon1.getwidth(this), icon1.getheight(this)); } }

java image swing embedded-resource

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -