日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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,加我的時候注明原因。

總結

以上是生活随笔為你收集整理的java制作的亲戚计算器(三姑六婆计算器)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。