-1

我想显示相关案例(网格中的第 2 行),我的 JLabel 包含在我的 Pawn 类中。

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

生成 Pawn 但在网格上,名为“label”的 JLabel 未显示。

编辑:我 纠正了一些事情,比如容器的使用,但是关于我的 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();
        }
    });
}
}
4

1 回答 1

1

我想指出我在您的代码中看到的两个主要问题(可能还有更多:))

  1. 在您的CoordButtons构造函数中,您正在做同样的事情 64 次。据我了解,您想创建一个 8x8 的网格。因此,将内容窗格布局设置为 8x8 网格并向其添加面板。

     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. 在您的CaseEchiquier班级中,仅创建一个Pawn对象不会帮助您显示它。而是将Pawn对象的标签添加到您的JPanel

       if(i==1 && (j>-1 && j<8)) { 
        Pawn p = new Pawn(colorr);
        add(p.label);
       }
    
于 2013-05-10T03:14:37.800 回答