无聊的时候写的一个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,加我的时候注明原因。

java制作的亲戚计算器(三姑六婆计算器)相关推荐

  1. java仿windows7计算器界面,java制作仿win7计算器之一计算器的图形界面的设计

    再结合网上各路神人及之前写的博客Swing编程方面步骤,如下: package calPack; import javax.swing.*; import java.awt.*; import jav ...

  2. Java制作计算器实现加减乘除

    Java制作计算器主要需要考虑的是加减乘除的运算顺序的优先级, 比如: 20-2-1.7*4/5-4*2 需要先计算1.7*4/5, 再计算4 *2, 最后从左往右计算加减. 网上其他的一些计算器都是 ...

  3. 【练习题】构造方法 编写Java程序,模拟简单的计算器。

    package day09; /*1.[练习题]构造方法 编写Java程序,模拟简单的计算器. 定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有.编写构造方法,赋予n1和n2初 ...

  4. 利用java swing编写一个简易的计算器,实现了括号,优先级,三角函数,阶乘等功能

    利用java swing编写一个简易的计算器 背景 效果图 一.默认图 二.计算三角函数 三.阶乘运算 四.常见的四则运算(实现了优先级) 代码 本文借鉴了"初识Java,实现简易计算器(带 ...

  5. java中用swing做一个windows计算器

    java中用swing做一个windows计算器 主函数 普通计数器 科学计算器 注意: 前言: 来看这篇教程估计都是java课程设计吧,现在已经没有公司很少使用swing组件了,java主要还是开发 ...

  6. Java制作VCARD

    转载自   Java制作VCARD 简介: vCard是电子名片的文件格式标准.它一般附加在电子邮件之后,但也可以用于其它场合(如在互联网上相互交换).vCard可包含的信息有:姓名.地址资讯.电话号 ...

  7. 用JAVA制作小游戏——飞机大战(三)

    本篇博客是对飞机大战游戏项目完整代码的展示 详细代码讲解: 用JAVA制作小游戏--飞机大战(一) 用JAVA制作小游戏--飞机大战(二) 最下方附整个程序的文件下载链接 代码展示 主界面 impor ...

  8. 用JAVA制作小游戏——飞机大战(二)

    本篇博客是对飞机大战游戏使用代码的展示 重难点: 首先需要鼠标能够控制战机,使鼠标在窗口内时始终能够使战机的位置与鼠标相同,实现鼠标控制战斗机移动. 其次需要能够以一定的速度产生子弹和敌机,并且以一定 ...

  9. 用JAVA制作小游戏——推箱子(三)

    本篇博客主要是对推箱子地图编辑器功能的代码讲解. 首先给出这段代码的部分运行截图: 重难点: 地图编辑器主要有三个重难点: 需要有一个绘制地图的界面 能够实现地图绘制的功能 地图绘制完成后需要将地图内 ...

最新文章

  1. 简易计算机单片机编程思路,到底以什么单片机入门?一些单片机简单的学习方法...
  2. sql server行级锁,排它锁,共享锁的使用
  3. 您能看出这个Double Check里的问题吗?
  4. 使用Spring WebFlux构建反应性REST API –第1部分
  5. mysql保存时乱码了_MySQL保存中文乱码的原因和解决办法
  6. BZOJ 1640: [Usaco2007 Nov]Best Cow Line 队列变换
  7. [轉]MS SQL 显示表结构
  8. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_2 响应之返回值是String类型...
  9. 为什么都不想去中科创达_那些过年不想回家的人,都去了哪?
  10. FastStone Capture7.0注册码
  11. 《VP9 Levels and Decoder Testing》笔记
  12. 伯努利分布、二项分布、多项分布、Beta分布、Dirichlet分布、连续分布(正态分布)、大数定理、中心极限定理、贝叶斯理论
  13. [渝粤教育] 九江职业技术学院 客户关系管理 参考 资料
  14. Android自定义日历控件
  15. 【文献译文】OFDM Receiver Using Deep Learning: Redundancy Issues
  16. 【2020总结】 一直坚持,一路向前!
  17. 我最喜欢的计算机课英语作文,我最喜欢的课外活动英语作文(通用10篇)
  18. ArrayIndexOutOfBoundsException: 4096 while reading gif file
  19. win10 计算机 搜索文件夹,win10如何搜索文件或文件夹_win10怎么全盘搜索文件-win7之家...
  20. 黑马程序员—[Android就业薪资] Android28期,毕业60天,就业率96.15%,平均薪水9378元!

热门文章

  1. deepnode软件下载地址_KeePass软件-KeePass下载地址
  2. win7注册表修改默认浏览器
  3. 今年春晚轮到谁来发红包?百度程序员:永不宕机符已备好
  4. 华为云 Tomcat配置https证书
  5. STM32的USART串口通讯程序
  6. 普通主播一个月能挣多少钱?结合身边的案例告诉你主播真实的收入!
  7. java支票金额转换
  8. BIM模型文件下载——某公司办公综合楼项目案例模型
  9. 揭秘中国16大暴利行业
  10. 常见的几种清除浮动(高度塌陷)的方法?