首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在代码中添加和删除JButtons

在代码中添加和删除JButtons
EN

Stack Overflow用户
提问于 2013-03-13 22:44:29
回答 1查看 320关注 0票数 1

因此,我是Java的新手,正在尝试使用JButtons为棋盘和棋子创建一个跳棋游戏。但是,我似乎无法通过ActionListener删除JButton。任何建议都将不胜感激。

代码语言:javascript
运行
复制
    public static void main(String[] args) {
    checkersBeBitchin begin = new checkersBeBitchin();

}

public checkersBeBitchin(){
    box.setLayout(new BorderLayout());
    makeBoard();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(600,600);
    setTitle("Checkers");


        }

private void makeBoard() {
    JPanel board = new JPanel();
    board.setLayout(new GridLayout(8,8));
    for (int i=0; i<8; i++){
        for (int j=0; j<8; j++) {
            squares[i][j] = new JButton();
            ActionListener actionListener = new Board();
            squares[i][j].addActionListener(actionListener);
            if((i%2 != 0 && j%2 !=0) ||(i%2==0 && j%2 == 0) ){
                    squares[i][j].setBackground(Color.black);
                    pieceTracker[i][j]=0;
                    //System.out.println("Black"+i+","+j); debugging
                    if(i<3){
                        int blue = 1;
                        Icon piece = new ImageIcon(getClass().getResource("/resources/piece.png"));
                        JButton button = new JButton(piece);
                        //squares[i][j].setRolloverIcon("image dir") to make it prettier down the road.
                        squares[i][j].add(button);
                        pieceTracker[i][j]=blue;
                        ActionListener Listener = new Blue();
                        button.addActionListener(Listener);
                        }
                    else if (i>4){
                        int red=-1;
                        Icon piece = new ImageIcon(getClass().getResource("/resources/piece2.png"));
                        JButton button = new JButton(piece);

                        squares[i][j].add(button);
                        pieceTracker[i][j]=red;
                        ActionListener Listener = new Red();
                        button.addActionListener(Listener);
                        //squares[i][j].setRolloverSelectedIcon("/resources/piece2alt.png");
                        }

            }
            else{
                squares[i][j].setBackground(Color.white);
                pieceTracker[i][j]=0;
                //System.out.println("White"+i+","+j); //debugging
                }
            board.add(squares[i][j]);

            }

        }
    box.add(board, BorderLayout.CENTER);
    }
private class Blue implements ActionListener{


    public void actionPerformed (ActionEvent e){
        System.out.println("You sexy Blue beast.");
        Object x = e.getSource();
        System.err.println(x);
        squares.remove(x);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-13 22:48:25

squares.remove?它应该显示为squares.remove(x)吗?我们能看看正方形的定义吗?它是一个数组吗?您必须将按钮从棋盘上移除,而不是从方块上移除,例如board.remove(x)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15388475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档