设计一个Java数字华容道程序1.设计并实现 GUI 系统。 2.使用基本参数文件存储系统参数,可通过系统对基本参数文件进行维护; 3.通过 GUI 操作实现软件功能,选择实现的窗口控件无限制。 4.软件可回显执行结果。
时间: 2024-02-24 17:54:24 浏览: 217
以下是一个简单的 Java 数字华容道程序,使用了 Swing 库实现 GUI 界面:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class DigitalHuarongdao extends JFrame implements ActionListener {
private JButton[][] button = new JButton[4][4]; // 数字华容道棋盘按钮
private JButton resetButton; // 重置按钮
private JTextArea resultArea; // 执行结果文本框
private File configFile = new File("config.txt"); // 参数配置文件
// 构造函数
public DigitalHuarongdao() {
super("数字华容道");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化数字华容道棋盘按钮
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
button[i][j] = new JButton();
button[i][j].setFont(new Font("宋体", Font.BOLD, 20));
button[i][j].setPreferredSize(new Dimension(80, 80));
button[i][j].addActionListener(this);
add(button[i][j]);
}
}
// 初始化重置按钮
resetButton = new JButton("重置");
resetButton.setFont(new Font("宋体", Font.BOLD, 20));
resetButton.setPreferredSize(new Dimension(100, 80));
resetButton.addActionListener(this);
add(resetButton);
// 初始化执行结果文本框
resultArea = new JTextArea();
resultArea.setFont(new Font("宋体", Font.PLAIN, 16));
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
scrollPane.setPreferredSize(new Dimension(400, 100));
add(scrollPane, BorderLayout.SOUTH);
// 加载参数配置文件
loadConfig();
// 显示窗口
setVisible(true);
}
// 实现 ActionListener 接口的 actionPerformed 方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetButton) { // 点击重置按钮
reset();
} else { // 点击数字华容道棋盘按钮
JButton clickedButton = (JButton)e.getSource();
int x = -1, y = -1;
// 查找点击的按钮在棋盘中的坐标
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (button[i][j] == clickedButton) {
x = i;
y = j;
break;
}
}
}
// 移动棋子
if (x > 0 && button[x-1][y].getText().equals("")) { // 上移
button[x-1][y].setText(clickedButton.getText());
clickedButton.setText("");
writeResult("上移 " + clickedButton.getText());
} else if (x < 3 && button[x+1][y].getText().equals("")) { // 下移
button[x+1][y].setText(clickedButton.getText());
clickedButton.setText("");
writeResult("下移 " + clickedButton.getText());
} else if (y > 0 && button[x][y-1].getText().equals("")) { // 左移
button[x][y-1].setText(clickedButton.getText());
clickedButton.setText("");
writeResult("左移 " + clickedButton.getText());
} else if (y < 3 && button[x][y+1].getText().equals("")) { // 右移
button[x][y+1].setText(clickedButton.getText());
clickedButton.setText("");
writeResult("右移 " + clickedButton.getText());
}
// 判断是否完成
if (isCompleted()) {
JOptionPane.showMessageDialog(this, "恭喜你,完成了数字华容道!", "完成", JOptionPane.INFORMATION_MESSAGE);
}
}
}
// 重置数字华容道棋盘
private void reset() {
// 读取参数配置文件中的初始棋盘状态
int[][] board = new int[4][4];
try {
BufferedReader reader = new BufferedReader(new FileReader(configFile));
for (int i = 0; i < 4; i++) {
String[] line = reader.readLine().split(",");
for (int j = 0; j < 4; j++) {
board[i][j] = Integer.parseInt(line[j]);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
// 设置棋盘按钮上的数字
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == 0) {
button[i][j].setText("");
} else {
button[i][j].setText(String.valueOf(board[i][j]));
}
}
}
// 清空执行结果文本框
resultArea.setText("");
}
// 判断数字华容道是否完成
private boolean isCompleted() {
int num = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (button[i][j].getText().equals("")) {
if (i == 3 && j == 3) {
return true;
} else {
return false;
}
} else if (Integer.parseInt(button[i][j].getText()) != num) {
return false;
}
num++;
}
}
return true;
}
// 加载参数配置文件
private void loadConfig() {
try {
BufferedReader reader = new BufferedReader(new FileReader(configFile));
for (int i = 0; i < 4; i++) {
String[] line = reader.readLine().split(",");
for (int j = 0; j < 4; j++) {
if (line[j].equals("0")) {
button[i][j].setText("");
} else {
button[i][j].setText(line[j]);
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
// 写入执行结果
private void writeResult(String result) {
resultArea.append(result + "\n");
resultArea.setCaretPosition(resultArea.getDocument().getLength());
}
// 主函数
public static void main(String[] args) {
new DigitalHuarongdao();
}
}
```
该程序实现了以下功能:
1. 显示数字华容道棋盘和重置按钮的 GUI 界面。
2. 可以通过读取参数配置文件来设置初始棋盘状态,并可通过 GUI 界面对参数配置文件进行维护。
3. 点击棋盘中的数字按钮可以移动棋子,并将移动结果显示在执行结果文本框中。
4. 点击重置按钮可以重置棋盘和执行结果文本框。
5. 判断数字华容道是否完成,并在完成时弹出提示框。
阅读全文
相关推荐


















