Placing component on Glass Pane

前端 未结 4 2120
时光说笑
时光说笑 2020-11-22 03:45

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any v

4条回答
  •  既然无缘
    2020-11-22 04:30

    Although tangential to the question, the JLayeredPane example cited by @camickr admits the following adaptation, which highlights the effect of mouseReleased() over an existing component.

    public ChessBoard() {
        ...
        // Add a few pieces to the board
        addPiece(3, 0, "♛"); 
        addPiece(4, 0, "♚");
        addPiece(3, 7, "♕");
        addPiece(4, 7, "♔");
    }
    
    static Font font = new Font("Sans", Font.PLAIN, 72);
    
    private void addPiece(int col, int row, String glyph) {
        JLabel piece = new JLabel(glyph, JLabel.CENTER);
        piece.setFont(font);
        JPanel panel = (JPanel) chessBoard.getComponent(col + row * 8);
        panel.add(piece);
    }
    

提交回复
热议问题