Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在代码中添加和删除jbutton_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java 在代码中添加和删除jbutton

Java 在代码中添加和删除jbutton,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,因此,我对Java比较陌生,并尝试使用JButtons为棋盘和棋子创建一个跳棋游戏。但是,我似乎无法通过ActionListener删除JButton。如有任何建议,将不胜感激 public static void main(String[] args) { checkersBeBitchin begin = new checkersBeBitchin(); } public checkersBeBitchin(){ box.setLayout(new BorderL

因此,我对Java比较陌生,并尝试使用JButtons为棋盘和棋子创建一个跳棋游戏。但是,我似乎无法通过ActionListener删除JButton。如有任何建议,将不胜感激

    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);
publicstaticvoidmain(字符串[]args){
checkerbebitchin begin=新checkerbebitchin();
}
公共支票{
box.setLayout(新的BorderLayout());
makeBoard();
setVisible(真);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(空);
设置大小(600600);
设置标题(“跳棋”);
}
私有void makeBoard(){
JPanel board=新的JPanel();
board.setLayout(新网格布局(8,8));

对于(int i=0;isquares.remove?它是否应该读取squares.remove(x)?我们可以看到正方形的定义吗?它是一个数组吗?您必须从板上而不是正方形上移除按钮,例如
BOARD.remove(x)

是的,很抱歉它是squares.remove(x);我只是在胡闹,忘了将其恢复正常,尽管这仍然不起作用。这就是我初始化方块的方式:JButton[]]squares=new JButton[8][8];还有board.remove除了int之外,x是一个对象。board是一个JPanel,还应该提供remove(对象)方法!顺便说一句:也许你想用jlabel替换按钮,而不是真的删除它?尝试board.remove(x)会给我一个错误:类型容器中的方法remove(int)不适用于参数(对象)。另外,我的计划是删除一个“片段”Jbutton,然后在我下一个单击的位置创建一个新按钮以进行跳棋。该计划还不错,不过出于布局原因,您可能希望用标签替换该按钮,而不是将其完全删除。