📄 game.java
字号:
package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Game extends JApplet implements ActionListener,DocumentListener,ItemListener{
/**
*
*/
//定义显示说明"世界大小"、"企业比例"、"合作者比例"、"刷新间隔"的标签
JLabel world,company,cooperate,refresh;
//定义"世界大小"、"企业比例"、"合作者比例"、"刷新间隔"标签相对应的输入框
JTextField worldText,companyText,cooperateText,refreshText;
//定义"参数设置"版面的JPanel容器
JPanel celluar;
//定义显示说明博弈规则中"合作者"、"竞争者"标签
JLabel coop1,coop2,enemy1,enemy2;
//定义博弈规则设置用的输入框
JTextField coop1Text,coop2Text,coop3Text,coop4Text,enemy1Text,enemy2Text,enemy3Text,enemy4Text;
//定义"博弈规则设置"版面的JPanel容器
JPanel rule;
//定义模式选择版面中的单选选项按钮
JRadioButton display1,display2,display3,neighbour1,neighbour2;
//定义实现单选按钮单选功能的集合
ButtonGroup displayGroupe,neighbourGroupe;
//定义"模式选择"版面的JPanel容器
JPanel modern;
//定义"确定"、"退出"、"设置默认参数"按钮
JButton ok,cancel,default_Celluar;
//定义"按钮控制"版面的JPanel容器
JPanel control;
//参数世界大小、刷新间隔、显示模式、邻居模式
private int worldSize,refreshTime,displayModern,neighbourModern;
//参数企业比例、合作者比例、博弈售益
private double companySize,coopSize,m;
//游戏模拟模块
JFrame modulate;
/**
* Constructor of the applet.
*
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
*/
public Game() {
super();
}
/**
* Called by the browser or applet viewer to inform
* this applet that it is being reclaimed and that it should destroy
* any resources that it has allocated. The <code>stop</code> method
* will always be called before <code>destroy</code>. <p>
*
* A subclass of <code>Applet</code> should override this method if
* it has any operation that it wants to perform before it is
* destroyed. For example, an applet with threads would use the
* <code>init</code> method to create the threads and the
* <code>destroy</code> method to kill them. <p>
*/
public void destroy() {
// Put your code here
}
/**
* Called by the browser or applet viewer to inform
* this applet that it has been loaded into the system. It is always
* called before the first time that the <code>start</code> method is
* called. <p>
*
* A subclass of <code>Applet</code> should override this method if
* it has initialization to perform. For example, an applet with
* threads would use the <code>init</code> method to create the
* threads and the <code>destroy</code> method to kill them. <p>
*/
//Applet初始化
public void init() {
// Put your code here
//图形界面初始化
guiInit();
}
public void guiInit(){
//Applet图形界面版面布局为4行1列
this.setLayout(new GridLayout(4,1));
//初始化参数设置界面
celluarGUI();
//将参数设置界面加入到Applet界面中
this.add(celluar);
//初始化博弈规则设置界面
ruleGUI();
//将博弈规则设置界面加入到Applet界面中
this.add(rule);
//初始化模式选择界面
modernGUI();
//将模式选择界面加入到Applet界面中
this.add(modern);
//初始化按钮控制界面
controlGUI();
//将按钮控制界面加入到Applet界面中
this.add(control);
//Applet界面设置默认参数
resetCelluar();
//设置Applet图形界面大小
this.setSize(new Dimension(300,450));
}
//初始化参数设置界面
private void celluarGUI(){
//初始化"世界大小"标签
world=new JLabel("世界大小");
//设置标签大小和位置
world.setBounds(new Rectangle(10, 30, 60, 20));
//初始化"世界大小"输入框
worldText=new JTextField();
//设置输入框的大小和位置
worldText.setBounds(new Rectangle(80, 30, 50, 20));
//初始化"企业比例"标签
company=new JLabel("企业比例");
//设置标签大小和位置
company.setBounds(new Rectangle(150, 30, 60, 20));
//初始化"企业比例"输入框
companyText=new JTextField();
//设置输入框的大小和位置
companyText.setBounds(new Rectangle(210, 30, 50, 20));
//初始化"合作者比例"标签
cooperate=new JLabel("合作者比例");
//设置标签大小和位置
cooperate.setBounds(new Rectangle(10, 60, 80, 20));
//初始化"合作者比例"输入框
cooperateText=new JTextField();
//设置输入框的大小和位置
cooperateText.setBounds(new Rectangle(80, 60, 50, 20));
//初始化"刷新间隔"标签
refresh=new JLabel("刷新间隔");
//设置标签大小和位置
refresh.setBounds(new Rectangle(150, 60, 60, 20));
//初始化"刷新间隔"输入框
refreshText=new JTextField();
//设置输入框的大小和位置
refreshText.setBounds(new Rectangle(210, 60, 50, 20));
//初始化JPanel容器
celluar=new JPanel();
//不使用版面控制器
celluar.setLayout(null);
//将图形控件加入JPanel容器中
celluar.add(world);
celluar.add(worldText);
celluar.add(company);
celluar.add(companyText);
celluar.add(companyText);
celluar.add(cooperate);
celluar.add(cooperateText);
celluar.add(refresh);
celluar.add(refreshText);
//设置JPanel容器的边框和标题
celluar.setBorder(BorderFactory.createTitledBorder("设置参数"));
}
//初始化"博弈规则"界面
private void ruleGUI(){
//初始化"合作"标签
coop1=new JLabel("合作");
//设置标签大小和位置
coop1.setBounds(new Rectangle(95, 20, 30, 20));
//初始化"合作"输入框
coop1Text=new JTextField();
//设置输入框大小和位置
coop1Text.setBounds(new Rectangle(90, 50, 30, 20));
//设置输入框不可编辑
coop1Text.setEnabled(false);
coop2=new JLabel("合作");
coop2.setBounds(new Rectangle(10, 50, 30, 20));
coop2Text=new JTextField();
coop2Text.setBounds(new Rectangle(50, 50, 30, 20));
coop2Text.setEnabled(false);
enemy1=new JLabel("竞争");
enemy1.setBounds(new Rectangle(185, 20, 30, 20));
coop3Text=new JTextField();
coop3Text.setBounds(new Rectangle(140, 50, 30, 20));
coop3Text.setEnabled(false);
enemy1Text=new JTextField();
enemy1Text.setBounds(new Rectangle(180, 50, 30, 20));
//输入框添加文本内容监听器
enemy1Text.getDocument().addDocumentListener(this);
enemy2=new JLabel("竞争");
enemy2.setBounds(new Rectangle(10, 80, 30, 20));
enemy2Text=new JTextField();
enemy2Text.setBounds(new Rectangle(50, 80, 30, 20));
enemy2Text.setEnabled(false);
coop4Text=new JTextField();
coop4Text.setBounds(new Rectangle(90, 80, 30, 20));
coop4Text.setEnabled(false);
enemy3Text=new JTextField();
enemy3Text.setBounds(new Rectangle(140, 80, 30, 20));
enemy3Text.setEnabled(false);
enemy4Text=new JTextField();
enemy4Text.setBounds(new Rectangle(180, 80, 30, 20));
enemy4Text.setEnabled(false);
//初始化JPanel容器
rule=new JPanel();
//不使用布局管理器
rule.setLayout(null);
//加入图形控件
rule.add(coop1);
rule.add(coop1Text);
rule.add(coop2);
rule.add(coop2Text);
rule.add(enemy1);
rule.add(coop3Text);
rule.add(enemy1Text);
rule.add(enemy2);
rule.add(enemy2Text);
rule.add(coop4Text);
rule.add(enemy3Text);
rule.add(enemy4Text);
//设置边框和标题
rule.setBorder(BorderFactory.createTitledBorder("设置博弈规则"));
}
//初始化模式选择界面
private void modernGUI(){
//初始化单选按钮"冯.扭曼型"
neighbour1=new JRadioButton("冯.扭曼型");
//初始化单选按钮"摩尔型"
neighbour2=new JRadioButton("摩尔型");
//初始化单选按钮集
neighbourGroupe=new ButtonGroup();
//单选按钮集中加入"冯.扭曼型"单选按钮
neighbourGroupe.add(neighbour1);
//单选按钮集中加入"摩尔型"单选按钮
neighbourGroupe.add(neighbour2);
//选按钮"冯.扭曼型"添加监听器
neighbour1.addItemListener(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -