java - How do I import images using ImageIcon? -
here's code, , i've got 2 problems evidently:
package test; import javax.swing.imageicon; import javax.swing.jpanel; import java.awt.component; public class { public static void main(string[] args) { imageicon icon = new imageicon("src/icon.png",("what great image")); jpanel window = new jpanel(); window.setlocation(100,100); window.setsize(300, 500); window.setvisible(true); }
so don't know why jpanel won't reveal itself, , importing image right? i've created icon , placed in src folder. if i'm not importing right, how import then? question applies graphics swing since i'm interested in learning soon.
so don't know why jpanel won't reveal itself
you've not added can displayed. jpanel
plain container allow add other components onto it.
you need kind of window
display container, example...
jpanel window = new jpanel(); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(window); frame.setsize(200, 200); frame.setlocationrelativeto(null); frame.setvisible(true);
have read through how make frames (main windows) more details
i've created icon , placed in src folder. if i'm not importing right, how import then?
that's complicated question, because you've added image src
directory, changes context of how image can loaded. essentially, becomes embedded resource.
so instead of using...
imageicon icon = new imageicon("src/icon.png",("what great image"));
you need use more like...
imageicon icon = new imageicon(something.getclass().getresource("icon.png"),("what great image"));
instead
you may find reading through how use labels, creating gui jfc/swing, initial threads, laying out components within container, reading/loading image helpful
Comments
Post a Comment