java制作的亲戚计算器(三姑六婆计算器)

无聊的时候写的一个swing应用。

直接上代码了,有些地方很乱,后面再看觉得可以精简的很多,因为是挺久之前写的,我也不想改了。

定义的数据文件格式那里有,程序计算功能要依赖于数据文件。

这是数据文件示例:其实就是把每个节点写作一行记录,一行一个节点。

[不存在]{(父-不存在)(母-不存在)(兄-不存在)(弟-不存在)(姐-不存在)(妹-不存在)(夫-不存在)(妻-不存在)(儿-不存在)(女-不存在)}
[你]{(父-爸爸)(母-妈妈)(兄-哥哥)(弟-弟弟)(姐-姐姐)(妹-妹妹)(夫-老公)(妻-老婆)(儿-儿子)(女-女儿)}
[爸爸]{(父-爷爷)(母-奶奶)(兄-伯伯)(弟-叔叔)(姐-姑姑)(妹-姑姑)(夫-不存在)(妻-妈妈)(儿-<哥哥><弟弟>)(女-<姐姐><妹妹>)}
[哥哥]{(父-爸爸)(母-妈妈)(兄-哥哥)(弟-<哥哥><弟弟>)(姐-姐姐)(妹-<姐姐><妹妹>)(夫-不存在)(妻-嫂子)(儿-侄子)(女-侄女)}
[老婆]{(父-岳父)(母-岳母)(兄-大舅)(弟-小舅)(姐-大姨)(妹-小姨)(夫-你)(妻-不存在)(儿-儿子)(女-女儿)}
预览图
【不存在】这条一定要有的

下面是所有代码:

package demo;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
/*
 * 介绍:关于“小”这个功能,在称呼存在年龄大小不同时,按钮会改变颜色,默认为比你大,点击后选择比你小。
 * 退格功能,就是返回上一步功能还没做,也不想做了。有兴趣自己看着加。
 * 当你能够确定关系输入过程中不存在年龄比较问题时,可以连续输入,不需要点“的”按钮。
 * 例如直接按“兄”->“姐”->“=”就可以得到答案 【姐姐】
 * 由于代码没有什么注释,所以比较难看,看不懂别问我,自己理解去。
 * 在这个函数initData()里面修改你的数据文件的位置。
 * 数据文件为一行一条数据(注意使用utf-8编码)。
 * 格式如下:[]{(父-)(母-)(兄-)(弟-)(姐-)(妹-)(夫-)(妻-)(儿-)(女-)}
 * 存在年龄比较时<称谓1><称谓2>,比你大的称谓在前面。
 */
public class QinQiJiSuanQi extends JFrame {
	static Hashtable<String, String> dataHashtable=new Hashtable<String, String>();
	StringSubClass ss=new StringSubClass();
	static String now ="你";
	static JTextPane inpuTextArea;
	static JTextArea resultTextArea;
	static JButton olderButton;
	static boolean isOlder=true;
 	QinQiJiSuanQi() {
		super("叫爸爸!");
		this.setLayout(null);
		//JPanel textpPanel=new JPanel();
		//textpPanel.setBackground(new Color(153, 187, 170));
		//textpPanel.setBounds(5, 10, 275, 100);
		//********文本域****************************************
		inpuTextArea=new JTextPane();
		inpuTextArea.setBounds(5, 10, 250, 50);
		inpuTextArea.setEditable(false);
		inpuTextArea.setBackground(new Color(153, 187, 170));
		inpuTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		resultTextArea=new JTextArea();
		resultTextArea.setBounds(5, 61, 250, 50);
		resultTextArea.setEditable(false);
		resultTextArea.setBackground(new Color(153, 187, 170));
		resultTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 35));
		resultTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		//******************下面是各种按钮***************************
		ButtonListener btnListener=new ButtonListener();
			//父 按钮
		JButton fatherButton=new JButton("父");
		fatherButton.setBounds(5, 120, 55, 55);
		fatherButton.setBackground(new Color(95, 95, 95));
		fatherButton.setForeground(Color.white);
		fatherButton.setFont(new Font("宋体", Font.BOLD, 20));
		fatherButton.addActionListener(btnListener);
			//母 按钮
		JButton motherButton=new JButton("母");
		motherButton.setBounds(70, 120, 55, 55);
		motherButton.setBackground(new Color(95, 95, 95));
		motherButton.setForeground(Color.white);
		motherButton.setFont(new Font("宋体", Font.BOLD, 20));
		motherButton.addActionListener(btnListener);
			// 退格 
		JButton backButton=new JButton("←");
		backButton.setBounds(135, 120, 55, 55);
		backButton.setBackground(new Color(217, 2, 0));
		backButton.setForeground(Color.white);
		backButton.setFont(new Font("宋体", Font.BOLD, 20));
		backButton.addActionListener(btnListener);
			//清零
		JButton acButton=new JButton("AC");
		acButton.setBounds(200, 120, 55, 55);
		acButton.setBackground(new Color(217, 2, 0));
		acButton.setForeground(Color.white);
		acButton.setFont(new Font("宋体", Font.BOLD, 16));
		acButton.addActionListener(btnListener);
			//兄
		JButton bortherButton=new JButton("兄");
		bortherButton.setBounds(5, 185, 55, 55);
		bortherButton.setBackground(new Color(95, 95, 95));
		bortherButton.setForeground(Color.white);
		bortherButton.setFont(new Font("宋体", Font.BOLD, 20));
		bortherButton.addActionListener(btnListener);
			//弟
		JButton littleBortherButton=new JButton("弟");
		littleBortherButton.setBounds(70, 185, 55, 55);
		littleBortherButton.setBackground(new Color(95, 95, 95));
		littleBortherButton.setForeground(Color.white);
		littleBortherButton.setFont(new Font("宋体", Font.BOLD, 20));
		littleBortherButton.addActionListener(btnListener);
			//夫
		JButton husbandButton=new JButton("夫");
		husbandButton.setBounds(135, 185, 55, 55);
		husbandButton.setBackground(new Color(95, 95, 95));
		husbandButton.setForeground(Color.white);
		husbandButton.setFont(new Font("宋体", Font.BOLD, 20));
		husbandButton.addActionListener(btnListener);
			//是否年长
		olderButton=new JButton("小");
		olderButton.setBounds(200, 185, 55, 55);
		olderButton.setBackground(new Color(217, 2, 0));
		olderButton.setForeground(Color.white);
		olderButton.setFont(new Font("宋体", Font.BOLD, 20));
		olderButton.addActionListener(btnListener);
			//姐
		JButton sisterButton=new JButton("姐");
		sisterButton.setBounds(5, 250, 55, 55);
		sisterButton.setBackground(new Color(95, 95, 95));
		sisterButton.setForeground(Color.white);
		sisterButton.setFont(new Font("宋体", Font.BOLD, 20));
		sisterButton.addActionListener(btnListener);
			//妹
		JButton littleSisterButton=new JButton("妹");
		littleSisterButton.setBounds(70, 250, 55, 55);
		littleSisterButton.setBackground(new Color(95, 95, 95));
		littleSisterButton.setForeground(Color.white);
		littleSisterButton.setFont(new Font("宋体", Font.BOLD, 20));
		littleSisterButton.addActionListener(btnListener);
			//妻
		JButton wifeButton=new JButton("妻");
		wifeButton.setBounds(135, 250, 55, 55);
		wifeButton.setBackground(new Color(95, 95, 95));
		wifeButton.setForeground(Color.white);
		wifeButton.setFont(new Font("宋体", Font.BOLD, 20));
		wifeButton.addActionListener(btnListener);
			//的
		JButton deButton=new JButton("的");
		deButton.setBounds(200, 250, 55, 55);
		deButton.setBackground(new Color(212, 98, 2));
		deButton.setForeground(Color.white);
		deButton.setFont(new Font("宋体", Font.BOLD, 20));
		deButton.addActionListener(btnListener);
			//子
		JButton sonButton=new JButton("儿");
		sonButton.setBounds(5, 315, 55, 55);
		sonButton.setBackground(new Color(95, 95, 95));
		sonButton.setForeground(Color.white);
		sonButton.setFont(new Font("宋体", Font.BOLD, 20));
		sonButton.addActionListener(btnListener);
			//女
		JButton daughterbButton=new JButton("女");
		daughterbButton.setBounds(70, 315, 55, 55);
		daughterbButton.setBackground(new Color(95, 95, 95));
		daughterbButton.setForeground(Color.white);
		daughterbButton.setFont(new Font("宋体", Font.BOLD, 20));
		daughterbButton.addActionListener(btnListener);
			//等于
		JButton resultButton=new JButton("=");
		resultButton.setBounds(135, 315, 120, 55);
		resultButton.setBackground(new Color(212, 98, 2));
		resultButton.setForeground(Color.white);
		resultButton.setFont(new Font("宋体", Font.BOLD, 40));
		resultButton.addActionListener(btnListener);
		//*********************************************************
		
		this.add(fatherButton);
		this.add(motherButton);
		this.add(backButton);
		this.add(acButton);
		this.add(bortherButton);
		this.add(littleBortherButton);
		this.add(husbandButton);
		this.add(olderButton);
		this.add(sisterButton);
		this.add(littleSisterButton);
		this.add(wifeButton);
		this.add(deButton);
		this.add(sonButton);
		this.add(daughterbButton);
		this.add(resultButton);
		//**以上为按钮**
		this.add(inpuTextArea);
		this.add(resultTextArea);
		this.setSize(280, 420);
		this.getContentPane().setBackground(new Color(51, 51, 51));
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		initData();
	}
	public static void main(String[] args) {
		new QinQiJiSuanQi();

	}
	void initData(){
		BufferedReader bfReader = null;
		try {
			bfReader=new BufferedReader( new InputStreamReader(new FileInputStream("data.txt"),"utf-8"));
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "数据文件不存在!");
		} catch (UnsupportedEncodingException e) {
			
		}
		String tempString;
		try {
			tempString= bfReader.readLine();
			while (tempString.length()>3) {
				dataHashtable.put(ss.subStringOne(tempString, "[", "]"), ss.subStringOne(tempString, "{", "}"));	
				tempString=bfReader.readLine();
			}//while
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "数据载入失败!");
		}catch (NullPointerException e) {
			System.out.println("数据载入完成->共加载数据"+dataHashtable.size()+"条");
			System.out.println("亲戚计算器 ver1.0  BY:huage2580");
		}
		
	}
	
}//class
class ButtonListener implements ActionListener{
	StringSubClass ss=new StringSubClass();
	public void actionPerformed(ActionEvent e) {
		String which=((JButton)e.getSource()).getText();
		if (which.equals("AC")) {
			QinQiJiSuanQi.now="你";
			QinQiJiSuanQi.isOlder=true;
			QinQiJiSuanQi.olderButton.setBackground(new Color(217, 2, 0));
			QinQiJiSuanQi.inpuTextArea.setText("");
			QinQiJiSuanQi.resultTextArea.setText("");
		}else if (which.equals("=")) {
			if (QinQiJiSuanQi.now.charAt(0)=='<') {
				String[] oldChooseString=ss.subStringAll(QinQiJiSuanQi.now, "<", ">");
				QinQiJiSuanQi.now=QinQiJiSuanQi.isOlder?oldChooseString[0]:oldChooseString[1];
				QinQiJiSuanQi.inpuTextArea.setText(QinQiJiSuanQi.inpuTextArea.getText()+(QinQiJiSuanQi.isOlder?"(比你年长)":"(比你年轻)"));
			}
			QinQiJiSuanQi.olderButton.setBackground(new Color(217, 2, 0));
			QinQiJiSuanQi.isOlder=true;
			QinQiJiSuanQi.resultTextArea.setText(QinQiJiSuanQi.now);
			//QinQiJiSuanQi.now="你";
		}else if (which.equals("小")) {
			QinQiJiSuanQi.isOlder=false;
			QinQiJiSuanQi.olderButton.setBackground(new Color(217, 2, 0));
		}else if(which.equals("←")){
			
		}else if(which.equals("的")){
			String temp="";
			QinQiJiSuanQi.olderButton.setBackground(new Color(217, 2, 0));
			if (QinQiJiSuanQi.now.charAt(0)=='<') {
				String[] oldChooseString=ss.subStringAll(QinQiJiSuanQi.now, "<", ">");
				QinQiJiSuanQi.now=QinQiJiSuanQi.isOlder?oldChooseString[0]:oldChooseString[1];
				temp=QinQiJiSuanQi.isOlder?"(比你年长)":"(比你年轻)";
			}
			QinQiJiSuanQi.inpuTextArea.setText(QinQiJiSuanQi.inpuTextArea.getText()+temp+which);
			QinQiJiSuanQi.isOlder=true;
		}else {//按下关系按钮时
			QinQiJiSuanQi.olderButton.setBackground(new Color(217, 2, 0));
			QinQiJiSuanQi.inpuTextArea.setText(QinQiJiSuanQi.inpuTextArea.getText()+which);
			QinQiJiSuanQi.now=getNext(which).substring(2);
			if (QinQiJiSuanQi.now.charAt(0)=='<') {
				QinQiJiSuanQi.olderButton.setBackground(new Color(212, 98, 2));
			}
			QinQiJiSuanQi.isOlder=true;
		}
		
	}
	String getNext(String what){
		String relationsString=QinQiJiSuanQi.dataHashtable.get(QinQiJiSuanQi.now);
		String[] relation = null;
		if (relationsString!=null) {
			relation =ss.subStringAll(relationsString, "(", ")");
		}else {
			QinQiJiSuanQi.resultTextArea.setText("超出计算范围");
			return QinQiJiSuanQi.now;
		}
		if (what.equals("父")) {
			return relation[0];
		}else if (what.equals("母")) {
			return relation[1];
		}else if (what.equals("兄")) {
			return relation[2];
		}else if (what.equals("弟")) {
			return relation[3];
		}else if (what.equals("姐")) {
			return relation[4];
		}else if (what.equals("妹")) {
			return relation[5];
		}else if (what.equals("夫")) {
			return relation[6];
		}else if (what.equals("妻")) {
			return relation[7];
		}else if (what.equals("儿")) {
			return relation[8];
		}else if (what.equals("女")) {
			return relation[9];
		}else {
			return QinQiJiSuanQi.now;
		}
		
	}
}//class

【记得更改数据文件】如果有什么问题的话欢迎留言,我近期才开始玩博客,估计很少上博客的,只有想到东西分享我才会上来看看。

需要我及时回复的话:企鹅号->2838666797,加我的时候注明原因。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值