2013-05-10 5 views
-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(); 
     } 
    }); 
} 
} 
+2

귀하의 질문은 단지 무엇을 우리에게 분명히하십시오. –

+0

8x8 격자의 각 셀에 왜'GridLayout (8, 8) '을 사용합니까? 이 레이아웃을 사용해야하는 셀을 보유하고있는 컨테이너 일 뿐이 아닌가? –

+0

컨테이너 및 8x8에 관한 문제를 수정했습니다. 제발 제발 제발 좀 찾아 주 시겠어요? 고마워 친구. – user2360545

답변

1

난 당신이 같은 일 64 시간을하고있는 당신의 CoordButtons 생성자에서

  1. (:) 더있을 수 있습니다) 난 당신의 코드에서 본 두 가지 문제를 지적하고 싶다. 내가 이해 한 바에 따르면8x8의 그리드를 만들고 싶습니다. 콘텐츠 창 레이아웃을 8x8 눈금으로 설정하고 패널을 에 추가하십시오. 단지 Pawn 객체를 생성하여 CaseEchiquier 클래스에서

    CoordBoutons() { 
         super("GridLayout"); 
         setDefaultCloseOperation(EXIT_ON_CLOSE);  
         getContentPane().setLayout(new GridLayout(8, 8)); 
         for (int i = 0; i < 8; i++) { 
          for (int j = 0; j < 8; j++) { 
           getContentPane().add(new CaseEchiquier(i, j)); 
          } 
         } 
         pack(); 
         setVisible(true); 
    } 
    
  2. 당신이 그것을 표시하는 데 도움이되지 않습니다. 대신 에 Pawn 개체의 레이블을 추가하여 JPanel

    if(i==1 && (j>-1 && j<8)) { 
        Pawn p = new Pawn(colorr); 
        add(p.label); 
        }