Java 调整JLayeredPane的图层大小

Java 调整JLayeredPane的图层大小,java,swing,java-7,jlayeredpane,jlayer,Java,Swing,Java 7,Jlayeredpane,Jlayer,我在尝试使用JLayeredPane制作自己的国际象棋游戏时遇到问题 我走了这么远: (希望您能看到blockG6在标签上有一个绿色边框,显示选择了哪个块) 但是,当我将我的2侧面板添加到我的棋盘面板中,然后在顶部有另一层,上面的标签应该完全覆盖每个棋盘块,但它没有: 如您所见,围绕块G2的绿色边框已关闭 我将侧面板s添加到棋盘中,并将其添加到底层,将其大小设置为600x600,然后添加顶层标签,该标签将精确匹配棋盘的块,绿色边框围绕选定的JLabel(以及下方的ChessBoardsqua

我在尝试使用
JLayeredPane
制作自己的国际象棋游戏时遇到问题

我走了这么远:

(希望您能看到block
G6
在标签上有一个绿色边框,显示选择了哪个块)

但是,当我将我的2
侧面板添加到我的
棋盘
面板中,然后在顶部有另一层,上面的标签应该完全覆盖每个棋盘块,但它没有:

如您所见,围绕块
G2
的绿色边框已关闭

我将
侧面板
s添加到
棋盘
中,并将其添加到底层,将其大小设置为600x600,然后添加顶层标签,该标签将精确匹配
棋盘
的块,绿色边框围绕选定的
JLabel
(以及下方的
ChessBoard
square[黑色或白色])绘制。由于
侧面板将棋盘的实际大小减小为600x600,因此它会偏离中心,现在它将是
600-sp1.getWidth()
x
600-sp2.getHeight()
。我试图设置边界和顶层的首选大小来补偿它,但似乎没有用。感谢您的帮助,谢谢:

ChessBoardTest.java:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ChessBoardTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Dimension boardSize = new Dimension(600, 600);

                JFrame frame = new JFrame("Chess JLayeredPane Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);

                Container contentPane = frame.getContentPane();

                ChessBoard chessBoard = new ChessBoard();
                SidePanel sp1 = new SidePanel(new String[]{"8", "7", "6", "5", "4", "3", "2", "1"}, SidePanel.VERTICAL);
                SidePanel sp2 = new SidePanel(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}, SidePanel.HORIZONTAL);

                //adding these 2 side panels messes up the layout
                chessBoard.add(sp1, BorderLayout.WEST);
                chessBoard.add(sp2, BorderLayout.SOUTH);

                chessBoard.setPreferredSize(boardSize);
                chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

                ChessPieceLayer chessPieceLayer = new ChessPieceLayer();

            //chessPieceLayer.setPreferredSize(boardSize);
            //chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

            //i've tried resizing to make up for the side panels but no result
            chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
            chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());




                JLayeredPane jLayeredPane = new JLayeredPane();
                jLayeredPane.setPreferredSize(boardSize);

                jLayeredPane.add(chessBoard, JLayeredPane.FRAME_CONTENT_LAYER);
                jLayeredPane.add(chessPieceLayer, JLayeredPane.MODAL_LAYER);

                contentPane.add(jLayeredPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
class SidePanel extends JPanel {

    final static String HORIZONTAL = "horizontal";
    final static String VERTICAL = "vertical";

    public SidePanel(String[] strings, String direction) {
        if (direction.equals(VERTICAL)) {
            setLayout(new GridLayout(8, 0));
        } else {
            setLayout(new GridLayout(0, 8));
        }
        setDoubleBuffered(true);
        for (String string : strings) {
            this.add(new JLabel(string, JLabel.CENTER));
        }

    }
}
class ChessBoard extends JPanel {

    public ChessBoard() {
        super(new BorderLayout(), true);

        this.add(populateBoard(Color.white, Color.black), BorderLayout.CENTER);
    }

    private JPanel populateBoard(Color c1, Color c2) {
        JPanel panel = new JPanel(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JPanel square = new JPanel(new BorderLayout());
                square.setBackground((i + j) % 2 == 0 ? c1 : c2);
                panel.add(square);
            }
        }
        return panel;
    }
}
class ChessPieceLayer extends JComponent {

    private HashMap<PiecePanel, String> panelsMap = new HashMap<>(64);
    final ChessPieceMouseListener listener;

    ChessPieceLayer() {
        super();
        listener = new ChessPieceMouseListener();
        setLayout(new GridLayout(8, 8));
        setDoubleBuffered(true);

        fillPanelsMap();
    }

    private void fillPanelsMap() {
        String[] cols = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
        int[] rows = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String row, col;
        int rowCount = 7, colCount = 0, trigger = 8;

        for (int i = 0; i < 64; i++) {

            if (trigger == 0) {
                colCount = 0;
                trigger = 8;
                rowCount--;
            }
            col = cols[colCount++];
            row = rows[rowCount] + "";
            trigger--;

            String location = col + row;

            PiecePanel square = createAndAddPiecesWithMouseListener(location);

            panelsMap.put(square, location);

        }
    }

    private PiecePanel createAndAddPiecesWithMouseListener(String location) {
        PiecePanel square = new PiecePanel(location, JLabel.CENTER);
        square.addMouseListener(listener);
        square.setText(location);
        this.add(square);
        return square;
    }
}
class PiecePanel extends JLabel {

    private String location;

    public PiecePanel(String text, int horizontalAlignment) {
        super("", horizontalAlignment);
        this.location = text;
    }

    PiecePanel(String location) {
        this.location = location;
    }

    public String getPieceLocation() {
        return location;
    }

    public void setPieceLocation(String location) {
        this.location = location;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
class ChessPieceMouseListener implements MouseListener {

    int counter = 0;
    PiecePanel this_pp, prev_pp;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        PiecePanel pp = (PiecePanel) e.getComponent();
        if (counter == 0) {
            this_pp = pp;
            this_pp.setBorder(new BevelBorder(0, Color.green, Color.green));
            JOptionPane.showMessageDialog(null, "From " + pp.getPieceLocation());
            counter = 1;
        } else {
            prev_pp = this_pp;
            this_pp = pp;
            prev_pp.setBorder(null);
            JOptionPane.showMessageDialog(null, "To " + pp.getPieceLocation());
            counter = 0;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
SidePanel.java:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ChessBoardTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Dimension boardSize = new Dimension(600, 600);

                JFrame frame = new JFrame("Chess JLayeredPane Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);

                Container contentPane = frame.getContentPane();

                ChessBoard chessBoard = new ChessBoard();
                SidePanel sp1 = new SidePanel(new String[]{"8", "7", "6", "5", "4", "3", "2", "1"}, SidePanel.VERTICAL);
                SidePanel sp2 = new SidePanel(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}, SidePanel.HORIZONTAL);

                //adding these 2 side panels messes up the layout
                chessBoard.add(sp1, BorderLayout.WEST);
                chessBoard.add(sp2, BorderLayout.SOUTH);

                chessBoard.setPreferredSize(boardSize);
                chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

                ChessPieceLayer chessPieceLayer = new ChessPieceLayer();

            //chessPieceLayer.setPreferredSize(boardSize);
            //chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

            //i've tried resizing to make up for the side panels but no result
            chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
            chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());




                JLayeredPane jLayeredPane = new JLayeredPane();
                jLayeredPane.setPreferredSize(boardSize);

                jLayeredPane.add(chessBoard, JLayeredPane.FRAME_CONTENT_LAYER);
                jLayeredPane.add(chessPieceLayer, JLayeredPane.MODAL_LAYER);

                contentPane.add(jLayeredPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
class SidePanel extends JPanel {

    final static String HORIZONTAL = "horizontal";
    final static String VERTICAL = "vertical";

    public SidePanel(String[] strings, String direction) {
        if (direction.equals(VERTICAL)) {
            setLayout(new GridLayout(8, 0));
        } else {
            setLayout(new GridLayout(0, 8));
        }
        setDoubleBuffered(true);
        for (String string : strings) {
            this.add(new JLabel(string, JLabel.CENTER));
        }

    }
}
class ChessBoard extends JPanel {

    public ChessBoard() {
        super(new BorderLayout(), true);

        this.add(populateBoard(Color.white, Color.black), BorderLayout.CENTER);
    }

    private JPanel populateBoard(Color c1, Color c2) {
        JPanel panel = new JPanel(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JPanel square = new JPanel(new BorderLayout());
                square.setBackground((i + j) % 2 == 0 ? c1 : c2);
                panel.add(square);
            }
        }
        return panel;
    }
}
class ChessPieceLayer extends JComponent {

    private HashMap<PiecePanel, String> panelsMap = new HashMap<>(64);
    final ChessPieceMouseListener listener;

    ChessPieceLayer() {
        super();
        listener = new ChessPieceMouseListener();
        setLayout(new GridLayout(8, 8));
        setDoubleBuffered(true);

        fillPanelsMap();
    }

    private void fillPanelsMap() {
        String[] cols = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
        int[] rows = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String row, col;
        int rowCount = 7, colCount = 0, trigger = 8;

        for (int i = 0; i < 64; i++) {

            if (trigger == 0) {
                colCount = 0;
                trigger = 8;
                rowCount--;
            }
            col = cols[colCount++];
            row = rows[rowCount] + "";
            trigger--;

            String location = col + row;

            PiecePanel square = createAndAddPiecesWithMouseListener(location);

            panelsMap.put(square, location);

        }
    }

    private PiecePanel createAndAddPiecesWithMouseListener(String location) {
        PiecePanel square = new PiecePanel(location, JLabel.CENTER);
        square.addMouseListener(listener);
        square.setText(location);
        this.add(square);
        return square;
    }
}
class PiecePanel extends JLabel {

    private String location;

    public PiecePanel(String text, int horizontalAlignment) {
        super("", horizontalAlignment);
        this.location = text;
    }

    PiecePanel(String location) {
        this.location = location;
    }

    public String getPieceLocation() {
        return location;
    }

    public void setPieceLocation(String location) {
        this.location = location;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
class ChessPieceMouseListener implements MouseListener {

    int counter = 0;
    PiecePanel this_pp, prev_pp;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        PiecePanel pp = (PiecePanel) e.getComponent();
        if (counter == 0) {
            this_pp = pp;
            this_pp.setBorder(new BevelBorder(0, Color.green, Color.green));
            JOptionPane.showMessageDialog(null, "From " + pp.getPieceLocation());
            counter = 1;
        } else {
            prev_pp = this_pp;
            this_pp = pp;
            prev_pp.setBorder(null);
            JOptionPane.showMessageDialog(null, "To " + pp.getPieceLocation());
            counter = 0;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
ChessBoard.java:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ChessBoardTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Dimension boardSize = new Dimension(600, 600);

                JFrame frame = new JFrame("Chess JLayeredPane Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);

                Container contentPane = frame.getContentPane();

                ChessBoard chessBoard = new ChessBoard();
                SidePanel sp1 = new SidePanel(new String[]{"8", "7", "6", "5", "4", "3", "2", "1"}, SidePanel.VERTICAL);
                SidePanel sp2 = new SidePanel(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}, SidePanel.HORIZONTAL);

                //adding these 2 side panels messes up the layout
                chessBoard.add(sp1, BorderLayout.WEST);
                chessBoard.add(sp2, BorderLayout.SOUTH);

                chessBoard.setPreferredSize(boardSize);
                chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

                ChessPieceLayer chessPieceLayer = new ChessPieceLayer();

            //chessPieceLayer.setPreferredSize(boardSize);
            //chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

            //i've tried resizing to make up for the side panels but no result
            chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
            chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());




                JLayeredPane jLayeredPane = new JLayeredPane();
                jLayeredPane.setPreferredSize(boardSize);

                jLayeredPane.add(chessBoard, JLayeredPane.FRAME_CONTENT_LAYER);
                jLayeredPane.add(chessPieceLayer, JLayeredPane.MODAL_LAYER);

                contentPane.add(jLayeredPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
class SidePanel extends JPanel {

    final static String HORIZONTAL = "horizontal";
    final static String VERTICAL = "vertical";

    public SidePanel(String[] strings, String direction) {
        if (direction.equals(VERTICAL)) {
            setLayout(new GridLayout(8, 0));
        } else {
            setLayout(new GridLayout(0, 8));
        }
        setDoubleBuffered(true);
        for (String string : strings) {
            this.add(new JLabel(string, JLabel.CENTER));
        }

    }
}
class ChessBoard extends JPanel {

    public ChessBoard() {
        super(new BorderLayout(), true);

        this.add(populateBoard(Color.white, Color.black), BorderLayout.CENTER);
    }

    private JPanel populateBoard(Color c1, Color c2) {
        JPanel panel = new JPanel(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JPanel square = new JPanel(new BorderLayout());
                square.setBackground((i + j) % 2 == 0 ? c1 : c2);
                panel.add(square);
            }
        }
        return panel;
    }
}
class ChessPieceLayer extends JComponent {

    private HashMap<PiecePanel, String> panelsMap = new HashMap<>(64);
    final ChessPieceMouseListener listener;

    ChessPieceLayer() {
        super();
        listener = new ChessPieceMouseListener();
        setLayout(new GridLayout(8, 8));
        setDoubleBuffered(true);

        fillPanelsMap();
    }

    private void fillPanelsMap() {
        String[] cols = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
        int[] rows = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String row, col;
        int rowCount = 7, colCount = 0, trigger = 8;

        for (int i = 0; i < 64; i++) {

            if (trigger == 0) {
                colCount = 0;
                trigger = 8;
                rowCount--;
            }
            col = cols[colCount++];
            row = rows[rowCount] + "";
            trigger--;

            String location = col + row;

            PiecePanel square = createAndAddPiecesWithMouseListener(location);

            panelsMap.put(square, location);

        }
    }

    private PiecePanel createAndAddPiecesWithMouseListener(String location) {
        PiecePanel square = new PiecePanel(location, JLabel.CENTER);
        square.addMouseListener(listener);
        square.setText(location);
        this.add(square);
        return square;
    }
}
class PiecePanel extends JLabel {

    private String location;

    public PiecePanel(String text, int horizontalAlignment) {
        super("", horizontalAlignment);
        this.location = text;
    }

    PiecePanel(String location) {
        this.location = location;
    }

    public String getPieceLocation() {
        return location;
    }

    public void setPieceLocation(String location) {
        this.location = location;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
class ChessPieceMouseListener implements MouseListener {

    int counter = 0;
    PiecePanel this_pp, prev_pp;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        PiecePanel pp = (PiecePanel) e.getComponent();
        if (counter == 0) {
            this_pp = pp;
            this_pp.setBorder(new BevelBorder(0, Color.green, Color.green));
            JOptionPane.showMessageDialog(null, "From " + pp.getPieceLocation());
            counter = 1;
        } else {
            prev_pp = this_pp;
            this_pp = pp;
            prev_pp.setBorder(null);
            JOptionPane.showMessageDialog(null, "To " + pp.getPieceLocation());
            counter = 0;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
ChessPieceMouseListener.java:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ChessBoardTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Dimension boardSize = new Dimension(600, 600);

                JFrame frame = new JFrame("Chess JLayeredPane Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);

                Container contentPane = frame.getContentPane();

                ChessBoard chessBoard = new ChessBoard();
                SidePanel sp1 = new SidePanel(new String[]{"8", "7", "6", "5", "4", "3", "2", "1"}, SidePanel.VERTICAL);
                SidePanel sp2 = new SidePanel(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}, SidePanel.HORIZONTAL);

                //adding these 2 side panels messes up the layout
                chessBoard.add(sp1, BorderLayout.WEST);
                chessBoard.add(sp2, BorderLayout.SOUTH);

                chessBoard.setPreferredSize(boardSize);
                chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

                ChessPieceLayer chessPieceLayer = new ChessPieceLayer();

            //chessPieceLayer.setPreferredSize(boardSize);
            //chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

            //i've tried resizing to make up for the side panels but no result
            chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
            chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());




                JLayeredPane jLayeredPane = new JLayeredPane();
                jLayeredPane.setPreferredSize(boardSize);

                jLayeredPane.add(chessBoard, JLayeredPane.FRAME_CONTENT_LAYER);
                jLayeredPane.add(chessPieceLayer, JLayeredPane.MODAL_LAYER);

                contentPane.add(jLayeredPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
class SidePanel extends JPanel {

    final static String HORIZONTAL = "horizontal";
    final static String VERTICAL = "vertical";

    public SidePanel(String[] strings, String direction) {
        if (direction.equals(VERTICAL)) {
            setLayout(new GridLayout(8, 0));
        } else {
            setLayout(new GridLayout(0, 8));
        }
        setDoubleBuffered(true);
        for (String string : strings) {
            this.add(new JLabel(string, JLabel.CENTER));
        }

    }
}
class ChessBoard extends JPanel {

    public ChessBoard() {
        super(new BorderLayout(), true);

        this.add(populateBoard(Color.white, Color.black), BorderLayout.CENTER);
    }

    private JPanel populateBoard(Color c1, Color c2) {
        JPanel panel = new JPanel(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JPanel square = new JPanel(new BorderLayout());
                square.setBackground((i + j) % 2 == 0 ? c1 : c2);
                panel.add(square);
            }
        }
        return panel;
    }
}
class ChessPieceLayer extends JComponent {

    private HashMap<PiecePanel, String> panelsMap = new HashMap<>(64);
    final ChessPieceMouseListener listener;

    ChessPieceLayer() {
        super();
        listener = new ChessPieceMouseListener();
        setLayout(new GridLayout(8, 8));
        setDoubleBuffered(true);

        fillPanelsMap();
    }

    private void fillPanelsMap() {
        String[] cols = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
        int[] rows = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String row, col;
        int rowCount = 7, colCount = 0, trigger = 8;

        for (int i = 0; i < 64; i++) {

            if (trigger == 0) {
                colCount = 0;
                trigger = 8;
                rowCount--;
            }
            col = cols[colCount++];
            row = rows[rowCount] + "";
            trigger--;

            String location = col + row;

            PiecePanel square = createAndAddPiecesWithMouseListener(location);

            panelsMap.put(square, location);

        }
    }

    private PiecePanel createAndAddPiecesWithMouseListener(String location) {
        PiecePanel square = new PiecePanel(location, JLabel.CENTER);
        square.addMouseListener(listener);
        square.setText(location);
        this.add(square);
        return square;
    }
}
class PiecePanel extends JLabel {

    private String location;

    public PiecePanel(String text, int horizontalAlignment) {
        super("", horizontalAlignment);
        this.location = text;
    }

    PiecePanel(String location) {
        this.location = location;
    }

    public String getPieceLocation() {
        return location;
    }

    public void setPieceLocation(String location) {
        this.location = location;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
class ChessPieceMouseListener implements MouseListener {

    int counter = 0;
    PiecePanel this_pp, prev_pp;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        PiecePanel pp = (PiecePanel) e.getComponent();
        if (counter == 0) {
            this_pp = pp;
            this_pp.setBorder(new BevelBorder(0, Color.green, Color.green));
            JOptionPane.showMessageDialog(null, "From " + pp.getPieceLocation());
            counter = 1;
        } else {
            prev_pp = this_pp;
            this_pp = pp;
            prev_pp.setBorder(null);
            JOptionPane.showMessageDialog(null, "To " + pp.getPieceLocation());
            counter = 0;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
起初,我使用:

chessPieceLayer.setPreferredSize(boardSize);
chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);
然后我试着:

chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());

但是结果没有差别

好的,我发现了问题,有一个简单的解决方案。下次,试着把你的代码放在一个类中,这样我就不必做太多的复制/粘贴(这种做法不鼓励人们尝试你的代码);-)

无论如何,问题来自侧面板,它占用了棋盘面板上的空间,因此引入了偏移量。解决方案非常简单,您只需:

  • sp1
    sp2
    添加到
    contentPane
  • 在您的
    棋盘
    棋子层
    上,只需将大小设置为boardSize
  • 解释

  • 我们将侧面板移动到内容窗格,这样它们就不会干扰您的层。使用JLayeredPane时,使用的是所谓的绝对布局或空布局。这意味着您必须注意组件的定位和大小。这就引出了第2点:
  • 当没有LayoutManager时,设置preferredSize是无用的,因为没有LayoutManager来调用该方法。现在,默认情况下,组件位于(0,0)中,因此您只需调用
    setSize
    。如果您希望对它们进行偏移,还需要调用
    setLocation
    ,或者在单个调用中将这两个调用连接到
    setBounds

  • 你能把棋盘和棋子玩家的代码贴出来吗?@GuillaumePolet都在那里。使用内部类,因此它们不会在自己的文件中声明,当然,仅用于测试。但我现在把它分开了,以便更容易查看。请看这个和。