-1

我正在使用 Java Swing 在 GUI 内创建战舰游戏。现在我只是想让这个网格正常工作,以便用户可以将战舰从网格底部拖放到上方网格内的某个位置。我想将 X 和 Y 整数值保存在 DropListener 类中,以便在将按钮放在网格上后,我可以检索 X 和 Y 值并相应地自动放置其余的船按钮(船的部分)分别将 1、2 和 3 添加到 X 值。

您需要为战舰、水和红色标记放入图片才能使其正常工作。

public class ButtonGrid extends JFrame

   JFrame frame=new JFrame(); //creates frame
   JButton[][] grid; //names the grid of buttons
    ImageIcon waterImage = new ImageIcon( "Water.png" );
    ImageIcon redImage = new ImageIcon( "Red.png" );
    ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
    ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
    ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
    ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

   public ButtonGrid(int width, int length) //constructor
   {
           frame.setLayout(new GridLayout(width,length)); //set layout
           grid=new JButton[width][length]; //allocate the size of grid
           for(int y=0; y<length; y++)
           {
                   for(int x=0; x<width-1; x++)
                   {
                           grid[x][y]=new JButton( waterImage ); //creates new button
                            grid[x][y].setBackground(Color.BLUE);
                            grid[x][y].setContentAreaFilled(false);
                            grid[x][y].setOpaque(true);
                            grid[x][y].setRolloverIcon( redImage );
                            grid[x][y].setFocusable(false);         
                            grid[x][y].setTransferHandler(new TransferHandler("icon"));

                            new DropListener(grid[x][y], x, y);

                           frame.add(grid[x][y]); //adds button to grid
                   }
            }

            //=================================================================BattleShip

                    grid[10][0]=new JButton( battleship1 ); //creates new button
                    grid[10][0].setBackground(Color.YELLOW);
                    grid[10][0].setContentAreaFilled(false);
                    grid[10][0].setOpaque(true);
                    MouseListener listener1 = new DragMouseAdapter();
                    grid[10][0].addMouseListener(listener1);
                    grid[10][0].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][0]); //adds button to grid

                    grid[10][1]=new JButton( battleship2 ); //creates new button
                    grid[10][1].setBackground(Color.YELLOW);
                    grid[10][1].setContentAreaFilled(false);
                    grid[10][1].setOpaque(true);
                    MouseListener listener2 = new DragMouseAdapter();
                    grid[10][1].addMouseListener(listener2);
                    grid[10][1].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][1]); //adds button to grid

                    grid[10][2]=new JButton( battleship3 ); //creates new button
                    grid[10][2].setBackground(Color.YELLOW);
                    grid[10][2].setContentAreaFilled(false);
                    grid[10][2].setOpaque(true);
                    MouseListener listener3 = new DragMouseAdapter();
                    grid[10][2].addMouseListener(listener3);
                    grid[10][2].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][2]); //adds button to grid

                    grid[10][3]=new JButton( battleship4 ); //creates new button
                    grid[10][3].setBackground(Color.YELLOW);
                    grid[10][3].setContentAreaFilled(false);
                    grid[10][3].setOpaque(true);
                    MouseListener listener4 = new DragMouseAdapter();
                    grid[10][3].addMouseListener(listener4);
                    grid[10][3].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][3]); //adds button to grid

            //=================================================================BattleShip



           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.pack(); //sets appropriate size for frame
           frame.setVisible(true); //makes frame visible
    } // end ButtonGrid constructor

    class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {               
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }

    } // end DragMouseAdapter

    class DropListener extends  EventListener, DropTargetListener
    {

        public DropListener(JButton[][] grid, int x, int y)
        {
            System.out.print( "X = " + x + " Y = " + y );
        }

        //void dropPerformed(DropEvent event, int x, int y)
        //{
        //  System.out.print( "X = " + x + " Y = " + y );
        //}
    }

   public static void main(String[] args)
   {
        new ButtonGrid(11,10);//makes new ButtonGrid with 2 parameters
    }

} // end ButtonGrid class
4

2 回答 2

2

首先,选择一个 API。您正在跨越 Drag'n'Drop API。您正在尝试使用TransferHandlerAPI 和核心 Drag'n'Drop API,它们不能以这种方式很好地混合(TransferHandlerAPI 使用核心 Drag'n'Drop API 来执行它们的操作)。

请查看拖放和数据传输以及如何使用 Java 2 进行拖放,第 1 部分以了解两者的介绍。

就您而言,就您个人而言,您不是移动组件,而是它们所代表的数据,这让我认为您想要使用TransferHandlerAPI。

这意味着您需要创建一个TransferHandler实现export以及import您尝试移动的数据。

基本思想是创建一个TransferHandler知道如何“导出”船舶和一个TransferHandler知道如何“导入”船舶数据(用于网格)的。

您为每个网格按钮创建一个“导入”处理程序,提供支持导入过程所需的信息,例如JButtonX/Y 网格信息。

对于“导出”处理程序,您只需要提供所需的“船舶”信息,以确保您知道该船舶将如何在网格中实现。

阅读上面链接的第一个教程以获取详细信息;)

于 2014-02-11T23:52:23.310 回答
0

抱歉,代码无法编译,您只需注释掉即可

new DropListener(grid[x][y], x, y);

连同 DropListener 类。但我取得了一点进展,就是这样。而不是使用

grid[x][y]=new JButton( waterImage ); //creates new button

为了创建我的按钮,我创建了自己的 Button 类并使用它来创建每个按钮

grid[x][y]=new GridButtonClass( x, y );

这允许我将 x 和 y 坐标传递给构造函数并保存它们!这是 GridButtonClass

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GridButtonClass extends JButton implements ActionListener{

int xValue;
int yValue;

ImageIcon waterImage = new ImageIcon( "Water.png" );
ImageIcon redImage = new ImageIcon( "Red.png" );
ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

public GridButtonClass(int x, int y){
    xValue = x;
    yValue = y;
    setIcon( waterImage );
    this.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
    System.out.println( "X Value = " + xValue + " Y Value = " + yValue );
    setIcon( redImage );
}

}

当您单击网格按钮时,图片将变为红色图像,其位置的 X 和 Y 值将出现在终端上。这是另一个文件,以防有人想全部执行。

public class ButtonGrid extends JFrame //, JButton implements ActionListener
{

   JFrame frame=new JFrame(); //creates frame
   JButton[][] grid; //names the grid of buttons
    ImageIcon waterImage = new ImageIcon( "Water.png" );
    ImageIcon redImage = new ImageIcon( "Red.png" );
    ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
    ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
    ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
    ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

   public ButtonGrid(int width, int length) //constructor
   {
           frame.setLayout(new GridLayout(width,length)); //set layout
           grid=new JButton[width][length]; //allocate the size of grid
           for(int y=0; y<length; y++)
           {
                   for(int x=0; x<width-1; x++)
                   {
                        grid[x][y]=new GridButtonClass( x, y );
                        //grid[x][y]=new JButton( waterImage ); //creates new button
                        grid[x][y].setBackground(Color.BLUE);
                        grid[x][y].setContentAreaFilled(false);
                        grid[x][y].setOpaque(true);
                        grid[x][y].setRolloverIcon( redImage );
                        grid[x][y].setFocusable(false);         
                        grid[x][y].setTransferHandler(new TransferHandler("icon"));

                        //new DropTargetListener();

                        frame.add(grid[x][y]); //adds button to grid
                   }
            }

            //=================================================================BattleShip

            grid[10][0]=new JButton( battleship1 ); //creates new button
            grid[10][0].setBackground(Color.YELLOW);
            grid[10][0].setContentAreaFilled(false);
            grid[10][0].setOpaque(true);
            MouseListener listener1 = new DragMouseAdapter();
            grid[10][0].addMouseListener(listener1);
            grid[10][0].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][0]); //adds button to grid

            grid[10][1]=new JButton( battleship2 ); //creates new button
            grid[10][1].setBackground(Color.YELLOW);
            grid[10][1].setContentAreaFilled(false);
            grid[10][1].setOpaque(true);
            MouseListener listener2 = new DragMouseAdapter();
            grid[10][1].addMouseListener(listener2);
            grid[10][1].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][1]); //adds button to grid

            grid[10][2]=new JButton( battleship3 ); //creates new button
            grid[10][2].setBackground(Color.YELLOW);
            grid[10][2].setContentAreaFilled(false);
            grid[10][2].setOpaque(true);
            MouseListener listener3 = new DragMouseAdapter();
            grid[10][2].addMouseListener(listener3);
            grid[10][2].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][2]); //adds button to grid

            grid[10][3]=new JButton( battleship4 ); //creates new button
            grid[10][3].setBackground(Color.YELLOW);
            grid[10][3].setContentAreaFilled(false);
            grid[10][3].setOpaque(true);
            MouseListener listener4 = new DragMouseAdapter();
            grid[10][3].addMouseListener(listener4);
            grid[10][3].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][3]); //adds button to grid

            //=================================================================BattleShip



           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.pack(); //sets appropriate size for frame
           frame.setVisible(true); //makes frame visible
    } // end ButtonGrid constructor

    class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {               
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }

    } // end DragMouseAdapter

   public static void main(String[] args)
   {
        new ButtonGrid(11,10);//makes new ButtonGrid with 2 parameters
    }

} // end ButtonGrid class

确保您添加自己的图像来代替我的图像。当你执行程序时你应该看到的是一个 10X10 的按钮网格,在这个网格下面有 4 个按钮组成了一艘战舰(我从网上裁剪了一张战舰图片,我希望我可以发布图片让你看看我的意思是)您可以根据需要多次将每个单独的船件拖放到网格上的任何位置。当您单击网格上的任意位置时,图片会变为红色,并且 X 和 Y 值会显示在终端上。

于 2014-02-13T01:24:49.213 回答