Java ImageIcon doesn't work -
i have troubles when try display image in jpanel
.
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class mainwindow { public static jframe mainframe; public static jpanel loginregisterpanel; public mainwindow() { mainframe(); loginregisterpanel(); } public void mainframe() { mainframe = new jframe(); mainframe.setsize(640, 480); mainframe.setvisible(true); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainframe.setresizable(false); } public void loginregisterpanel() { loginregisterpanel = new jpanel(); loginregisterpanel.setlayout(null); mainframe.add(loginregisterpanel); jbutton loginbutton = new jbutton("login"); jbutton registerbutton = new jbutton("register"); /*imageicon logoimage = new imageicon("resource/logo.jpg"); jlabel logoimagelabel = new jlabel(); logoimagelabel.setbounds(0, 0, 640, 200); logoimagelabel.seticon(logoimage); loginregisterpanel.add(logoimagelabel); */ loginbutton.setbounds(260, 180, 120, 50); loginregisterpanel.add(loginbutton); loginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { try { loginregisterpanel.setvisible(false); loginpanel loginpanel = new loginpanel(); mainframe.getcontentpane().add(loginpanel.loginp); } catch (exception ce){ ce.printstacktrace(); } } }); registerbutton.setbounds(260, 250, 120, 50); loginregisterpanel.add(registerbutton); } public static void main (string[] args) { mainwindow mainwindow = new mainwindow(); } }
if delete section imageicon
source code, jpanel
displays buttons, if use imageicon
, doesn't display anything.
loginregisterpanel.setlayout(null);
causing problem. guess absence of layout manager confuses java put all. should give gridlayout perhaps.
Comments
Post a Comment