java - Showing a JLabel -
i want show on concerned cases (row 2 grid), jlabel contained in pawn class.
if(i==1 && (j>-1 && j<8)) { new pawn(colorr); } generate pawn on grid, jlabel named 'label' isn't showed.
edit:i corrected things container usage problem jlabel showing , move pawn piece still here.
i enjoy move later pawn position on grid.
package coordboutons; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class coordboutons extends jframe { jframe frame; private color colorr=color.red; //private container[][] cp=new container[8][8]; coordboutons() { super("gridlayout"); setdefaultcloseoperation(exit_on_close); container contenant = getcontentpane(); contenant.setlayout(new gridlayout(8, 8)); (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { contenant.add(new caseechiquier(i, j)); } } pack(); setvisible(true); } class caseechiquier extends jpanel { private int lin, col; protected color color; caseechiquier(int i, int j) { lin = i; col = j; setpreferredsize(new dimension(80, 75)); setbackground((i + j) % 2 == 0 ? color.white : color.gray); if(i==1 && (j>-1 && j<8)) { new pawn(colorr); } addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e){ caseechiquier current =(caseechiquier)e.getsource(); // object user pressed // int linx = current.getlin(); // int coly = current.getcol(); system.out.println(lin+" "+col); } }); } public int getcol() { return col; } public int getlin() { return lin; } } public class chesspiece { color color; jlabel label; } public class pawn extends chesspiece { public pawn(color c) { this.color = c; setbackground(colorr); system.out.println("yataaa !"); this.label = new jlabel(new imageicon("bp.png")); //i need show label !; } public color getcolor() { return this.color; } } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { jframe.setdefaultlookandfeeldecorated(true); coordboutons coordboutons = new coordboutons(); } }); } }
i point out 2 major problems saw in code (there can more :))
in
coordbuttonsconstructor doing same thing 64 times. according understood want create grid of 8x8. set content pane layout 8x8 grid , add panels it.coordboutons() { super("gridlayout"); setdefaultcloseoperation(exit_on_close); getcontentpane().setlayout(new gridlayout(8, 8)); (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { getcontentpane().add(new caseechiquier(i, j)); } } pack(); setvisible(true); }in
caseechiquierclass creatingpawnobject not display it. instead add label ofpawnobjectjpanelif(i==1 && (j>-1 && j<8)) { pawn p = new pawn(colorr); add(p.label); }
Comments
Post a Comment