DropListener检索X&在拖放到JButton [] [] Grid之后,来自JButton [X] [Y]的Y.

时间:2014-02-11 23:35:53

标签: java swing jbutton actionevent

我正在使用Java Swing在GUI中创建一个战舰游戏。目前我只是想让这个网格工作,以便用户可以将战舰从网格底部拖放到上面网格中的一个点上。我想保存X&在DropListener类中的Y整数值,以便在网格上删除按钮后,我可以检索X& Y值并通过分别向X值添加1,2和3来自动放置其余的船按钮(船的各个部分)。

你需要为战舰,水和红色标记投入照片才能发挥作用。

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

2 个答案:

答案 0 :(得分:2)

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

请查看Drag and Drop and Data TransferHow to drag and drop with Java 2, Part 1,了解两者。

在您的情况下,就个人而言,您不是移动组件,而是他们所代表的数据,这让我觉得您想要使用TransferHandler API。

这意味着您需要创建一个TransferHandler实施,exportimport您要移动的数据。

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

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

对于“导出”处理程序,您只需提供所需的“发货”信息,以确保您知道该船在网格中的实施方式。

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

答案 1 :(得分: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 );
}

}

单击网格按钮时,图片将变为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值将显示在终端上。