我正在使用 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