Basic Java Swing - adding JLabels with icons at runtime -
i'm making grid containing icons representing game map, , grid need redrawn. i'm working way basics. here's code got working
int = 1; while (i < 50) { pnlmap.add(new jlabel(string.valueof(i))); += 1; } now want jlabels display icons, can't figure out syntax arguments on pnl.add()
i imagine it's like
pnlmap.add(new jlabel("").seticon(new imageicon(clientgui.class .getresource("/resources/wall.jpg"))));
as can guess doesn't work. error: the method add(component) in type container not applicable arguments (void)
how above code add jlabels icons?
(on separate note, what's kind of object construction called, "add new jlabel" dynamically rather initialising before?)
unlike constructor jlabel, seticon function doesn't return (or returns void). means code looks bit this:
pnlmap.add(void); which why error being thrown.
therefore, slight modification of code needed make loop work.
int = 1; while (i < 50) { jlabel label = new jlabel(string.valueof(i)); label.seticon(new imageicon(clientgui.class .getresource("/resources/wall.jpg"))); pnlmap.add(label); += 1; } edit: in answer question new jlabel() construction in code. is, surprisingly, called dynamic object construction.
Comments
Post a Comment