package Calculator; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Calculator { TempData td = new TempData(); Frame frame = new Frame("帅OTTO计算器"); TextArea tf = new TextArea(3, 40); Button b0 = new Button("0"); Button b1 = new Button("1"); Button b2 = new Button("2"); Button b3 = new Button("3"); Button b4 = new Button("4"); Button b5 = new Button("5"); Button b6 = new Button("6"); Button b7 = new Button("7"); Button b8 = new Button("8"); Button b9 = new Button("9"); Button bc = new Button("c"); Button be = new Button("="); Button ba = new Button("+"); Button bm = new Button("-"); Button bt = new Button("*"); Button bd = new Button("/"); Panel p1 = new Panel(); Panel p2 = new Panel(); Box box1 = Box.createHorizontalBox(); MenuBar mb = new MenuBar(); Menu option = new Menu("选项"); Menu ColorChoose = new Menu("背景颜色"); MenuItem Color_Red = new MenuItem("红色"); MenuItem Color_Yellow = new MenuItem("黄色"); MenuItem Color_green = new MenuItem("绿色"); MenuItem Color_white = new MenuItem("白色"); public void InitWindows() { Panel panel = new Panel(); panel.add(tf); frame.add(panel, BorderLayout.NORTH); p1.setLayout(new GridLayout(3, 4)); p1.add(b0); p1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5); p1.add(b6); p1.add(b7); p1.add(b8); p1.add(b9); p1.add(bc); p1.add(be); p2.setLayout(new GridLayout(2, 2)); p2.add(ba); p2.add(bm); p2.add(bt); p2.add(bd); box1.add(p1); box1.add(p2); frame.add(box1, BorderLayout.SOUTH); ColorChoose.add(Color_Red); ColorChoose.add(Color_Yellow); ColorChoose.add(Color_green); ColorChoose.add(Color_white); option.add(ColorChoose); mb.add(option); frame.setMenuBar(mb); frame.setBounds(500, 250, 161 * 3, 300); frame.setVisible(true); tf.setText("只支持十以内的计算"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); Color_Red.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.RED); } }); Color_Yellow.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.YELLOW); } }); Color_green.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.green); } }); Color_white.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.white); } }); b0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("0"); } else { tf.append("0"); } td.t1 = "0"; } }); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("1"); } else { tf.append("1"); } td.t1 = "1"; } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("2"); } else { tf.append("2"); } td.t1 = "2"; } }); b3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("3"); } else { tf.append("3"); } td.t1 = "3"; } }); b4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("4"); } else { tf.append("4"); } td.t1 = "4"; } }); b5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("5"); } else { tf.append("5"); } td.t1 = "5"; } }); b6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("6"); } else { tf.append("6"); } td.t1 = "6"; } }); b7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("7"); } else { tf.append("7"); } td.t1 = "7"; } }); b8.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("8"); } else { tf.append("8"); } td.t1 = "8"; } }); b9.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (td.t2 == "") { tf.setText("9"); } else { tf.append("9"); } td.t1 = "9"; } }); be.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // int t1 = Integer.parseInt(td.t1); // int t2 = Integer.parseInt(td.t2); // int t3 = 0; Double t1 = Double.parseDouble(td.t1); Double t2 = Double.parseDouble(td.t2); Double t3 = 0.0; if (td.f == 1) { t3 = t1 + t2; tf.setText(t3 + ""); } else if (td.f == 2) { t3 = t2 - t1; tf.setText(t3 + ""); } else if (td.f == 3) { t3 = t1 * t2; tf.setText(t3 + ""); } else if (td.f == 4) { if (t1 != 0) { t3 = t2 / t1; tf.setText(t3 + ""); } else { td.t1 = ""; td.t2 = ""; td.f = 0; tf.setText("除数不为0"); } } td.t1 = t3+""; } }); bc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { td.t1 = ""; td.t2 = ""; td.f = 0; tf.setText(""); } }); ba.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tf.append("+"); td.f = 1; td.t2 = td.t1; td.t1 = ""; } }); bm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tf.append("-"); td.f = 2; td.t2 = td.t1; td.t1 = ""; } }); bt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tf.append("*"); td.f = 3; td.t2 = td.t1; td.t1 = ""; } }); bd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tf.append("/"); td.f = 4; td.t2 = td.t1; td.t1 = ""; } }); } public static void main(String[] args) throws FileNotFoundException { new Calculator().InitWindows(); } }
(2)TempData类:
1 2 3 4 5 6 | package Calculator; public class TempData { String t1 = "" ; String t2 = "" ; int f = 0 ; } |
运行截图:
改动部分
(1):添加外部包jlayer:
(2):添加MP3素材:
(3)添加新增的类
OTTO_Play类:完成媒体播放,由博士编写。
Soul_Soother类:随机播放几句心灵鸡汤,用于测试媒体播放和多线程是否实现,保证媒体播放时,可以进行其他操作,体现了多线程的特点。由博士提供想法和编写。
Button_Voice类:实现按键播音,按1说1,按2说2,就跟超市里的计算器一样,由博士编写。
(4):类的具体内容:
OTTO_Play类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package src.Calculator; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; import java.io.FileInputStream; import java.io.FileNotFoundException; public class OTTO_Play extends Thread { FileInputStream fis = null ; String url = "" ; public OTTO_Play(String u) throws FileNotFoundException { this .url = u; fis = new FileInputStream(u); } public OTTO_Play(){ } public void run() { try { Player player = new Player(fis); player.play(); } catch (JavaLayerException e) { throw new RuntimeException(e); } } } |
Soul_Soother类:
package src.Calculator; import javazoom.jl.decoder.JavaLayerException; import java.io.FileNotFoundException; import java.util.Random; public class Soul_Soother { OTTO_Play op; String url; public String Comfort() throws FileNotFoundException, JavaLayerException { Random random = new Random(); int m = random.nextInt(5); switch (m) { case 0: url = "media/otto_waao.mp3"; op = new OTTO_Play(url); op.start(); return ("哇袄!"); case 1: url = "media/otto_guancanhai.mp3"; op = new OTTO_Play(url); op.start(); return ("老骥伏枥,志在千里;烈士暮年,壮心不已。"); case 2: url = "media/otto_qianli.mp3"; op = new OTTO_Play(url); op.start(); return ("你蕴含着无限的潜力,发掘它会得到意想不到的结果。"); case 3: url = "media/otto_rensheng.mp3"; op = new OTTO_Play(url); op.start(); return ("人生重要的不是所站的位置,而是所朝的方向人的弱小和强大是靠战胜自我来决定的。"); case 4: url = "media/otto_yifrenz.mp3"; op = new OTTO_Play(url); op.start(); return ("你也许只花了一分钟去选择,但你可能要用一生的时间去忏悔。"); case 5: url = "media/otto_ruguoni.mp3"; op = new OTTO_Play(url); op.start(); return ("如果你生气了,请在面对爱人之前先面对镜子。看看自己,你喜欢现在这张脸吗?"); } return null; } }
Button_Voice类:
package src.Calculator; import java.io.FileNotFoundException; public class Button_Voice { OTTO_Play op; public void ButtonVoid(String u) throws FileNotFoundException { op = new OTTO_Play(u); op.start(); } }
(5)主类新改部分(已标红):
package src.Calculator; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Calculator { Soul_Soother ss = new Soul_Soother(); Button_Voice bv = new Button_Voice(); TempData td = new TempData(); Frame frame = new Frame("帅OTTO计算器"); TextArea tf = new TextArea(3, 40); Button b0 = new Button("0"); Button b1 = new Button("1"); Button b2 = new Button("2"); Button b3 = new Button("3"); Button b4 = new Button("4"); Button b5 = new Button("5"); Button b6 = new Button("6"); Button b7 = new Button("7"); Button b8 = new Button("8"); Button b9 = new Button("9"); Button bc = new Button("c"); Button be = new Button("="); Button ba = new Button("+"); Button bm = new Button("-"); Button bt = new Button("*"); Button bd = new Button("/"); Panel p1 = new Panel(); Panel p2 = new Panel(); Box box1 = Box.createHorizontalBox(); MenuBar mb = new MenuBar(); Menu option = new Menu("选项"); Menu ColorChoose = new Menu("背景颜色"); MenuItem Color_Red = new MenuItem("红色"); MenuItem Color_Yellow = new MenuItem("黄色"); MenuItem Color_green = new MenuItem("绿色"); MenuItem Color_white = new MenuItem("白色"); MenuItem SoulSoup = new MenuItem("来句心灵鸡汤"); public void InitWindows() { Panel panel = new Panel(); panel.add(tf); frame.add(panel, BorderLayout.NORTH); p1.setLayout(new GridLayout(3, 4)); p1.add(b0); p1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5); p1.add(b6); p1.add(b7); p1.add(b8); p1.add(b9); p1.add(bc); p1.add(be); p2.setLayout(new GridLayout(2, 2)); p2.add(ba); p2.add(bm); p2.add(bt); p2.add(bd); box1.add(p1); box1.add(p2); frame.add(box1, BorderLayout.SOUTH); ColorChoose.add(Color_Red); ColorChoose.add(Color_Yellow); ColorChoose.add(Color_green); ColorChoose.add(Color_white); option.add(ColorChoose); mb.add(option); option.add(SoulSoup); frame.setMenuBar(mb); frame.setBounds(500, 250, 161 * 3, 300); frame.setVisible(true); tf.setText("只支持十以内的计算"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); Color_Red.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.RED); } }); Color_Yellow.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.YELLOW); } }); Color_green.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.green); } }); Color_white.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setBackground(Color.white); } }); SoulSoup.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { tf.setText(ss.Comfort()); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } catch (JavaLayerException ex) { throw new RuntimeException(ex); } } }); b0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/0.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("0"); } else { tf.append("0"); } td.t1 = "0"; } }); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/1.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("1"); } else { tf.append("1"); } td.t1 = "1"; } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/2.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("2"); } else { tf.append("2"); } td.t1 = "2"; } }); b3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/3.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("3"); } else { tf.append("3"); } td.t1 = "3"; } }); b4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/4.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("4"); } else { tf.append("4"); } td.t1 = "4"; } }); b5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/5.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("5"); } else { tf.append("5"); } td.t1 = "5"; } }); b6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/6.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("6"); } else { tf.append("6"); } td.t1 = "6"; } }); b7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/7.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("7"); } else { tf.append("7"); } td.t1 = "7"; } }); b8.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/8.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("8"); } else { tf.append("8"); } td.t1 = "8"; } }); b9.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/9.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } if (td.t2 == "") { tf.setText("9"); } else { tf.append("9"); } td.t1 = "9"; } }); be.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/=.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } // int t1 = Integer.parseInt(td.t1); // int t2 = Integer.parseInt(td.t2); // int t3 = 0; Double t1 = Double.parseDouble(td.t1); Double t2 = Double.parseDouble(td.t2); Double t3 = 0.0; if (td.f == 1) { t3 = t1 + t2; tf.setText(t3 + ""); } else if (td.f == 2) { t3 = t2 - t1; tf.setText(t3 + ""); } else if (td.f == 3) { t3 = t1 * t2; tf.setText(t3 + ""); } else if (td.f == 4) { if (t1 != 0) { t3 = t2 / t1; tf.setText(t3 + ""); } else { td.t1 = ""; td.t2 = ""; td.f = 0; tf.setText("除数不为0"); } } td.t1 = t3+""; } }); bc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/c.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } td.t1 = ""; td.t2 = ""; td.f = 0; tf.setText(""); } }); ba.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/+.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } tf.append("+"); td.f = 1; td.t2 = td.t1; td.t1 = ""; } }); bm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/-.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } tf.append("-"); td.f = 2; td.t2 = td.t1; td.t1 = ""; } }); bt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/×.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } tf.append("*"); td.f = 3; td.t2 = td.t1; td.t1 = ""; } }); bd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { bv.ButtonVoid("media/÷.mp3"); } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } tf.append("/"); td.f = 4; td.t2 = td.t1; td.t1 = ""; } }); } public static class OTTO_Come implements Runnable { @Override public void run() { FileInputStream fis; { try { fis = new FileInputStream("media\\otto_1.mp3"); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } Player py = null; try { py = new Player(fis); } catch (JavaLayerException e) { throw new RuntimeException(e); } try { py.play(); } catch (JavaLayerException e) { throw new RuntimeException(e); } try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } public static void main(String[] args) throws FileNotFoundException { new Thread(new OTTO_Come()).start(); new Calculator().InitWindows(); } }
结果视频录制:
思考与总结:
小利:
我在编程过程中,负责代码主体的思路构建和主类编写,由于在上一学期刚刚学习完java,对java熟练度较高便采用java语言来进行此次结对编程,与博士的合作过程期间,在我负责编写代码时,博士在一旁和我一同讨论钻研,时而对我的代码提出建议,在博士编写过程中,我也会给他提供帮助,一同优化代码。此次结对编程加强了我代码编写的能力,也考验了我与同伴之间的配合能力,作为软件工程的一名学生,在未来就业时极大可能就是负责团队之间的软件开发,这一次结对编程可以作为一次历练,为后续的学习与工作提供莫大的帮助。
博士:
由于小利对于代码的熟练程度较高,我个人在本次结对编程中主要就是与他讨论设计内容、学习他的代码构建思路,在编程过程中我负责了几个类的撰写, 撰写过程中,小利会在一旁监督并指导我的程序代码编写,对我编程过程中产出的疑问尽力解答,在编程出错时也会给我提供帮助,和我一起思考代码的优化。这次结对编程,不仅是考验我个人的代码编撰能力,也是对我们团队之间的合作互助进行了考验,主旨是希望我们能够互帮互助,互相学习,在此次团队合作中我受益匪浅,很感谢小利对我的帮助,也感谢他个人为团队的付出。
特别标注与鸣谢:
(1)与我结对编程的队友“博士”
(2)jlayer包的下载地址:Download jlayer-1.0.1-1.jar : jlayer « j « Jar File Download (java2s.com)
(3)jlayer包使用教程:Java播放MP3——JLayer-CSDN博客
(4)Oracle官方文档:Frame (Java Platform SE 8 ) (oracle.com)
(5)音效素材生成网站:在线生成otto鬼叫 (wzq02.cf)
(6)音效格式转化网站:MP4转MP3 - 在线转换音频文件 (aconvert.com)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?