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

📄 controller.java

📁 我这次还是java的代码
💻 JAVA
字号:
/**
 * 
 */
package hnu.software.pfdai;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * @author daipengfei
 * 
 */
public class Controller extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final int DEFAULT_WIDTH = 600;

	public static final int DEFAULT_HEIGHT = 400;

	private Model model;

	/**
	 * Constructs a new Controller that is initially invisible.
	 * 
	 */
	public Controller() {
		super("Guess The Car");

		model = new Model();
		TextView tView = new TextView();
		model.addObserver(tView);
		tView.update(model, null);
		tView.addActionListener(this);

		ResultView rView = new ResultView();
		model.addObserver(rView);
		rView.update(model, null);

		GraphicsView gView = new GraphicsView();
		model.addObserver(gView);
		gView.update(model, null);

		setLayout(new GridLayout(1, 2));
		JPanel c = new JPanel();
		c.setLayout(new GridLayout(2, 1));
		c.add(tView);
		c.add(rView);
		add(c);

		add(gView);

		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/**
	 * deal with the enter action matter
	 */
	public void actionPerformed(ActionEvent e) {
		if (model.getTimes() == 0) {
			JTextField t = (JTextField) e.getSource();
			String count = t.getText();			
			if(!count.matches("[1-9]+")){
				count = "-1";
			}
			int times = Integer.parseInt(count);
			model.setTimes(times + 1);
		} else {
			JRadioButton rb = (JRadioButton) e.getSource();
			String rbText = rb.getText();
			if (rbText.equals("No change")) {
				model.setChoice(0);
			} else if (rbText.equals("Random choice")) {
				model.setChoice(1);
			} else if (rbText.equals("Choose another door"))
				model.setChoice(2);
		}
	}

	/**
	 * main
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Controller frame = new Controller();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

⌨️ 快捷键说明

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