⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 用JAVA开发的模拟企业间竞争与合作策略的生命周期游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		//单选按钮"摩尔型"添加监听器
		neighbour2.addItemListener(this);
		//初始化"邻居模式"JPanel容器
		JPanel neighboer=new JPanel();
		//设置为2行1列的布局管理
		neighboer.setLayout(new GridLayout(2,1));
		//加入单选按钮
		neighboer.add(neighbour1);
		neighboer.add(neighbour2);
		//设置容器的边框和标题
		neighboer.setBorder(BorderFactory.createTitledBorder("邻居模式选择"));
		//初始化单选按钮"企业ID"
		display1=new JRadioButton("企业ID");
		//初始化单选按钮"移动方向"
		display2=new JRadioButton("移动方向");
		//初始化单选按钮"企业策略"
		display3=new JRadioButton("企业策略");
		//初始化单选按钮集
		displayGroupe=new ButtonGroup();
		//单选按钮集中加入"企业ID"单选按钮
		displayGroupe.add(display1);
		//单选按钮集中加入"移动方向"单选按钮
		displayGroupe.add(display2);
		//单选按钮集中加入"企业策略"单选按钮
		displayGroupe.add(display3);
		//单选按钮"企业ID"添加监听器
		display1.addItemListener(this);
		//单选按钮"移动方向"添加监听器
		display2.addItemListener(this);
		//单选按钮"企业策略"添加监听器
		display3.addItemListener(this);
		//初始化"显示模式"JPanel容器
		JPanel display=new JPanel();
		//加入单选按钮
		display.add(display1);
		display.add(display2);
		display.add(display3);
		//设置3行1列的布局管理
		display.setLayout(new GridLayout(3,1));
		//设置容器的边框标题
		display.setBorder(BorderFactory.createTitledBorder("显示模式选择"));
		//初始化"模式选择"JPanel容器
		modern=new JPanel();
		//设置1行2列的布局管理
		modern.setLayout(new GridLayout(1,2));
		//加入"邻居模式"容器
		modern.add(neighboer);
		//加入"显示模式"容器
		modern.add(display);
		//设置边框和标题
		modern.setBorder(BorderFactory.createTitledBorder("模式选择"));
	}
	//初始化"按钮控制"版面
	private void controlGUI(){
		//初始化"确定"按钮
		ok=new JButton("确定");
		//"确定"按钮添加事件监听器
		ok.addActionListener(this);
		cancel=new JButton("退出");
		cancel.addActionListener(this);
		default_Celluar=new JButton("恢复默认参数");
		default_Celluar.addActionListener(this);
		//初始化"按钮控制"容器
		control=new JPanel();
        //加入按钮
		control.add(ok);
		control.add(cancel);
		control.add(default_Celluar);
		
		
	}
	//设置默认参数
	private void resetCelluar(){
		worldText.setText(Integer.toString(DefaultCelluar.WorldSize));
		companyText.setText(Double.toString(DefaultCelluar.Company));
		cooperateText.setText(Double.toString(DefaultCelluar.Cooperate));
		refreshText.setText(Integer.toString(DefaultCelluar.Refresh));
		coop1Text.setText(Integer.toString(1));
		coop2Text.setText(Integer.toString(1));
		coop3Text.setText(Integer.toString(0));
		enemy1Text.setText(Double.toString(DefaultCelluar.M));
		enemy2Text.setText(Double.toString(DefaultCelluar.M));
		coop4Text.setText(Integer.toString(0));
		enemy3Text.setText(Integer.toString(0));
		enemy4Text.setText(Integer.toString(0));
		//设置选择"冯.扭曼型"单选按钮
		neighbour1.setSelected(true);
		//设置选择"企业策略"单选按钮
		display3.setSelected(true);
	}
	//设置参数,将图形界面中的值赋给相应的参数
	private void setParmaters(){
		worldSize=Integer.parseInt(worldText.getText());
		companySize=Double.parseDouble(companyText.getText());
		coopSize=Double.parseDouble(cooperateText.getText());
		refreshTime=Integer.parseInt(refreshText.getText());
		m=Double.parseDouble(enemy1Text.getText());
	}
	
	private void displayParmater(){
		System.out.println("w:"+worldSize);
		System.out.println("com:"+companySize);
		System.out.println("c:"+coopSize);
		System.out.println("t:"+refreshTime);
		System.out.println("m:"+m);	
		System.out.println("n:"+neighbourModern);
		System.out.println("d:"+displayModern);
	}

	/**
	 * Called by the browser or applet viewer to inform
	 * this applet that it should start its execution. It is called after
	 * the <code>init</code> method and each time the applet is revisited
	 * in a Web page. <p>
	 *
	 * A subclass of <code>Applet</code> should override this method if
	 * it has any operation that it wants to perform each time the Web
	 * page containing it is visited. For example, an applet with
	 * animation might want to use the <code>start</code> method to
	 * resume animation, and the <code>stop</code> method to suspend the
	 * animation. <p>
	 */
	public void start() {
		// Put your code here
	}

	/**
	 * Called by the browser or applet viewer to inform
	 * this applet that it should stop its execution. It is called when
	 * the Web page that contains this applet has been replaced by
	 * another page, and also just before the applet is to be destroyed. <p>
	 *
	 * A subclass of <code>Applet</code> should override this method if
	 * it has any operation that it wants to perform each time the Web
	 * page containing it is no longer visible. For example, an applet
	 * with animation might want to use the <code>start</code> method to
	 * resume animation, and the <code>stop</code> method to suspend the
	 * animation. <p>
	 */
	public void stop() {
		// Put your code here
	}

	public  static void main(String args[]){
		new Game();
	}
    //捕捉按钮点击事件
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
		//若点击"退出"按钮
		if(e.getSource()==cancel){
			//退出游戏
			System.exit(0);
		}
		//若点击"恢复默认参数"按钮
		if(e.getSource()==default_Celluar){
			//图形界面相应位置设为默认值
			resetCelluar();
		}
		////若点击"确定"按钮
		if(e.getSource()==ok){
			//参数赋值
			setParmaters();
			//若游戏正在模拟
			if(modulate!=null){
				//关闭游戏模拟模块
				modulate.dispose();
				//重新开始游戏模拟
				modulate=new Modulate(this);
			}else{
				//开始游戏模拟
				modulate=new Modulate(this);
			}
			
			
			
		}
	}

	//捕捉输入框文本内容变化事件
	public void changedUpdate(DocumentEvent e) {
		// TODO 自动生成方法存根
		
	}
    //捕捉输入框文本内容插入事件
	public void insertUpdate(DocumentEvent e) {
		// TODO 自动生成方法存根
		//将对应的输入框的文本值相应变化
		if(e.getDocument()==enemy1Text.getDocument()){
			enemy2Text.setText(enemy1Text.getText());
		}
		
	}
    //捕捉输入框文本内容删除事件
	public void removeUpdate(DocumentEvent e) {
		// TODO 自动生成方法存根
		////将对应的输入框的文本值相应变化
		if(e.getDocument()==enemy1Text.getDocument()){
			enemy2Text.setText(enemy1Text.getText());
		}
		
	}
    //捕捉单选按钮事件
	public void itemStateChanged(ItemEvent e) {
		// TODO 自动生成方法存根
		//选择了哪个单选按钮,为相应的参数赋值
		if(e.getStateChange()==e.SELECTED){
			if(e.getSource()==neighbour1){
				neighbourModern=1;
			}
			if(e.getSource()==neighbour2){
				neighbourModern=2;
			}
			if(e.getSource()==display1){
				displayModern=1;
			}
			if(e.getSource()==display2){
				displayModern=2;
			}
			if(e.getSource()==display3){
				displayModern=3;
			}
		}
		
	}

	public double getCompanySize() {
		return companySize;
	}

	public double getCoopSize() {
		return coopSize;
	}

	public int getDisplayModern() {
		return displayModern;
	}

	public double getM() {
		return m;
	}

	public int getNeighbourModern() {
		return neighbourModern;
	}

	public int getRefreshTime() {
		return refreshTime;
	}

	public int getWorldSize() {
		return worldSize;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -