java农夫过河图形界面代码
时间: 2023-07-02 10:05:35 浏览: 111
数据结构-农夫过河代码
以下是一个简单的 Java 农夫过河图形界面代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FarmerGame extends JFrame implements ActionListener {
private JButton farmer;
private JButton wolf;
private JButton goat;
private JButton cabbage;
private JButton goButton;
private JLabel message;
private JPanel leftBank;
private JPanel rightBank;
private boolean farmerLeft = true;
private boolean wolfLeft = true;
private boolean goatLeft = true;
private boolean cabbageLeft = true;
public FarmerGame() {
super("农夫过河");
// 创建组件
farmer = new JButton("农夫");
wolf = new JButton("狼");
goat = new JButton("羊");
cabbage = new JButton("白菜");
goButton = new JButton("前往对岸");
message = new JLabel("请将所有物品都带到对岸");
leftBank = new JPanel();
leftBank.setBorder(BorderFactory.createTitledBorder("左岸"));
leftBank.setLayout(new GridLayout(2, 2));
leftBank.add(farmer);
leftBank.add(wolf);
leftBank.add(goat);
leftBank.add(cabbage);
rightBank = new JPanel();
rightBank.setBorder(BorderFactory.createTitledBorder("右岸"));
rightBank.setLayout(new GridLayout(2, 2));
// 添加组件
add(leftBank, BorderLayout.WEST);
add(rightBank, BorderLayout.EAST);
add(goButton, BorderLayout.SOUTH);
add(message, BorderLayout.NORTH);
// 添加事件监听器
farmer.addActionListener(this);
wolf.addActionListener(this);
goat.addActionListener(this);
cabbage.addActionListener(this);
goButton.addActionListener(this);
// 设置窗口大小、位置和关闭方式
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == goButton) {
// 判断是否成功到达对岸
if (!farmerLeft && !wolfLeft && !goatLeft && !cabbageLeft) {
message.setText("恭喜你成功到达对岸!");
return;
}
// 判断是否有物品被狼吃掉
if (!farmerLeft && wolfLeft && !goatLeft && !cabbageLeft) {
message.setText("狼吃掉了羊!");
return;
}
// 判断是否有物品被羊吃掉
if (!farmerLeft && !wolfLeft && goatLeft && !cabbageLeft) {
message.setText("羊吃掉了白菜!");
return;
}
// 判断是否有物品被农夫单独带走
if (farmerLeft && !wolfLeft && goatLeft && !cabbageLeft) {
message.setText("农夫单独带走了羊!");
return;
}
// 判断是否有物品被农夫带走后狼吃掉
if (farmerLeft && !wolfLeft && !goatLeft && cabbageLeft) {
message.setText("农夫带走了白菜,狼吃掉了羊!");
return;
}
// 判断是否有物品被农夫带走后羊吃掉
if (farmerLeft && wolfLeft && !goatLeft && !cabbageLeft) {
message.setText("农夫带走了羊,羊吃掉了白菜!");
return;
}
// 判断是否有物品被农夫带走后狼和羊吃掉
if (farmerLeft && !wolfLeft && !goatLeft && !cabbageLeft) {
message.setText("农夫带走了狼和羊,狼吃掉了羊!");
return;
}
// 判断是否可以前往对岸
if (farmerLeft) {
message.setText("农夫必须在场!");
return;
}
if (wolfLeft && !goatLeft && !cabbageLeft) {
message.setText("狼会吃羊!");
return;
}
if (!wolfLeft && goatLeft && cabbageLeft) {
message.setText("羊会吃白菜!");
return;
}
// 前往对岸
farmerLeft = false;
if (wolfLeft) {
wolfLeft = false;
leftBank.remove(wolf);
rightBank.add(wolf);
} else {
wolfLeft = true;
rightBank.remove(wolf);
leftBank.add(wolf);
}
if (goatLeft) {
goatLeft = false;
leftBank.remove(goat);
rightBank.add(goat);
} else {
goatLeft = true;
rightBank.remove(goat);
leftBank.add(goat);
}
if (cabbageLeft) {
cabbageLeft = false;
leftBank.remove(cabbage);
rightBank.add(cabbage);
} else {
cabbageLeft = true;
rightBank.remove(cabbage);
leftBank.add(cabbage);
}
leftBank.remove(farmer);
rightBank.add(farmer);
message.setText("请在对岸选择物品带回来");
} else if (e.getSource() == farmer) {
if (farmerLeft) {
leftBank.remove(farmer);
rightBank.add(farmer);
} else {
rightBank.remove(farmer);
leftBank.add(farmer);
}
} else if (e.getSource() == wolf) {
if (farmerLeft && wolfLeft) {
leftBank.remove(wolf);
rightBank.add(wolf);
wolfLeft = false;
} else if (!farmerLeft && !wolfLeft) {
rightBank.remove(wolf);
leftBank.add(wolf);
wolfLeft = true;
}
} else if (e.getSource() == goat) {
if (farmerLeft && goatLeft) {
leftBank.remove(goat);
rightBank.add(goat);
goatLeft = false;
} else if (!farmerLeft && !goatLeft) {
rightBank.remove(goat);
leftBank.add(goat);
goatLeft = true;
}
} else if (e.getSource() == cabbage) {
if (farmerLeft && cabbageLeft) {
leftBank.remove(cabbage);
rightBank.add(cabbage);
cabbageLeft = false;
} else if (!farmerLeft && !cabbageLeft) {
rightBank.remove(cabbage);
leftBank.add(cabbage);
cabbageLeft = true;
}
}
}
public static void main(String[] args) {
new FarmerGame();
}
}
```
注意:该代码只是一个简单的农夫过河游戏,可能存在一些不完善之处。如有需要,请自行进行修改和完善。
阅读全文