我已经使用Java创建了一个Checkers游戏界面,并将棋子放置在游戏板上的每个位置,但是目前,我无法将棋子从一个正方形移动到另一个正方形。
如何在棋盘游戏中移动跳棋?
以下是我的程序的屏幕截图: file:///home/stown/Pictures/Screenshot%20from%202019-08-28%2011-56-51.png
下面是我尝试过的源代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chackergame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
/**
*
* @author wenda
*/
public class CheckerGame extends JFrame {
private final int ROWS = 8;
private final int COLL = 8;
private final JPanel[][] square = new JPanel[8][8];
private JPanel backgroundPanel;
private final JLabel statusBar = new JLabel(" Red turn to play");
private JLabel lbPieces = new JLabel();
private boolean inDrag = false;
public CheckerGame() {
//Add a chess board to the Layered Pane
backgroundPanel = new JPanel();
backgroundPanel.setLayout(new GridLayout(8, 8, 0, 0));
add(backgroundPanel, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
createSquare();
addPieces();
setIconImage(PiecesIcon.iconGame.getImage());
setTitle("Java Checkers");// set title game
setSize(500, 500);
//setResizable(false);
setLocationRelativeTo(null);
setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void createSquare() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLL; col++) {
JPanel cell = new JPanel();
if ((row + col) % 2 == 1) {
cell.setBackground(new Color(99, 164, 54));
// cell.setBackground(Color.BLACK);
}
if ((row + col) % 2 == 0) {
cell.setBackground(new Color(247, 235, 164));
// cell.setBackground(Color.WHITE);
}
square[row][col] = cell;
backgroundPanel.add(square[row][col]);
}
}
}
/**
* Add pieces to the board
*/
private void addPieces() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLL; col++) {
lbPieces = new JLabel();
lbPieces.setHorizontalAlignment(SwingConstants.CENTER);
//lb.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
if ((row + col) % 2 == 1) {
if (row < 3) {
lbPieces.setIcon(PiecesIcon.redPiece);
} else if (row > 4) {
lbPieces.setIcon(PiecesIcon.whitePiece);
}
}
square[row][col].setLayout(new BorderLayout());
square[row][col].add(lbPieces, BorderLayout.CENTER);
} // end of for col loop
} // end of for row loop
} // end of addPieces method
public class MouseInput extends MouseAdapter {
@Override
public void mousePressed(MouseEvent evt) {
}
@Override
public void mouseReleased(MouseEvent evt) {
}
@Override
public void mouseClicked(MouseEvent evt) {
}
@Override
public void mouseDragged(MouseEvent evt) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new CheckerGame();
}
}
答案 0 :(得分:0)
基本问题是关于将组件(在这种情况下为List<String> list = Arrays.asList("hdfs://NNcluster/finalsnappy/2019/4/8291/table_8291_69_2019_04_01", "hdfs://NNcluster/finalsnappy/2019/4/8291/table_8291_69_2019_04_02");
list.forEach(fileName-> {
SparkSession spark = get_spark_session();// gets spark session
Dataset<Row> tmpDS = spark.read().format("parquet").load(fileName);
tmpDS.show();
System.out.println(tmpDS.count());
});
)从任何源容器(一个JLable
)拖动到任何其他容器。
每个此类容器都可以是所拖动组件的来源,也可以是其放置目标。因此它需要同时支持。这样的JPanel
在下面的JPanel
类中实现。
为了编写这段代码,我从MadProgrammer's answer借来了,它显示了一个源和一个目标的解决方案。
它是一个文件mre:可以将整个代码复制粘贴到DragDropPane
文件中并运行。它演示了如何在面板之间拖动LabelDnD.java
。请注意以下注释:
JLabel