-1
내 Pawn 클래스에 포함 된 내 JLabel 관련 사례 (그리드의 2 행)를 표시하고 싶습니다. JLabel 표시
if(i==1 && (j>-1 && j<8)) { new Pawn(colorr); }
는 폰을 생성하지만, 그리드에서 JLabel의 이름을 '레이블'보여되지 않습니다.
편집 : I컨테이너 사용과 같은 몇 가지 사항은 수정되었지만 JLabel의 표시 및 내 Pawn 조각 이동에 대한 문제점은 여전히 여기에 있습니다.
나는 Pawn을 그리드의 다른 위치로 옮기는 것을 즐긴다.
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));
for (int i = 0; i < 8; i++) {
for (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(); // get the object that the 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 to show this 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();
}
});
}
}
귀하의 질문은 단지 무엇을 우리에게 분명히하십시오. –
8x8 격자의 각 셀에 왜'GridLayout (8, 8) '을 사용합니까? 이 레이아웃을 사용해야하는 셀을 보유하고있는 컨테이너 일 뿐이 아닌가? –
컨테이너 및 8x8에 관한 문제를 수정했습니다. 제발 제발 제발 좀 찾아 주 시겠어요? 고마워 친구. – user2360545