java使用工厂模式编写计算器(swing界面)
来源:互联网 发布:电视会被网络取代吗 编辑:程序博客网 时间:2024/06/05 08:33
使用工厂模式编写计算器
首先我们先有算法这个接口
1:算法接口
package com.v4;public class Arithmetic { public int count(String str){ return 0; }}
2:算法工厂类
package com.v4;public class ArithmeticFactory {public static Arithmetic getArith(String str){ Arithmetic ari = null; if(str.indexOf("+") != -1){ ari = new Add(); }else if(str.indexOf("-") != -1){ ari = new Minus(); }else if(str.indexOf("*") != -1){ ari = new Multiply(); }else if(str.indexOf("/") != -1){ ari = new Divide(); } return ari; }}
3:相加类
package com.v4;public class Add extends Arithmetic{ @Override public int count(String str) { String[] strs = str.split("\\+"); return Integer.parseInt(strs[0])+Integer.parseInt(strs[1]); }}
4:相减类
package com.v4;public class Minus extends Arithmetic{ @Override public int count(String str) { // TODO Auto-generated method stub String[] strs = str.split("-"); return Integer.parseInt(strs[0])-Integer.parseInt(strs[1]); }
5:相乘类
package com.v4;public class Multiply extends Arithmetic{ @Override public int count(String str) { // TODO Auto-generated method stub String[] strs = str.split("\\*"); return Integer.parseInt(strs[0])*Integer.parseInt(strs[1]); }}
6:相除类
package com.v4;public class Divide extends Arithmetic{ @Override public int count(String str) { // TODO Auto-generated method stub String[] strs = str.split("/"); return Integer.parseInt(strs[0])/Integer.parseInt(strs[1]); }}
7:swing页面类
package com.v4;import java.util.Scanner;package com.v4;import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import java.awt.Color;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JLabel;import java.awt.event.FocusAdapter;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class Calculator extends JFrame implements ActionListener { private JPanel contentPane; private JTextField textField; JButton button_1; JButton button_2; JButton button_3; JButton button_4; JButton button_5; JButton button_6; JButton button_7; JButton button_8; JButton button_9; JButton button_0; JButton button_add; JButton button_minus; JButton button_mulptily; JButton button_divide; JButton button_clear; JButton button_equal; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculator frame = new Calculator(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Calculator() { setBackground(Color.WHITE); setTitle("计算器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.CENTER); panel.setLayout(null); textField = new JTextField(); textField.setBounds(12, 12, 412, 36); textField.setHorizontalAlignment(SwingConstants.RIGHT); panel.add(textField); textField.setColumns(10); button_4 = new JButton("4"); button_4.setBounds(12, 103, 85, 31); button_4.addActionListener(this); panel.add(button_4); button_5 = new JButton("5"); button_5.setBounds(120, 103, 85, 31); button_5.addActionListener(this); panel.add(button_5); button_1 = new JButton("1"); button_1.setBounds(12, 60, 85, 31); button_1.addActionListener(this); panel.add(button_1); button_6 = new JButton("6"); button_6.setBounds(231, 103, 85, 31); button_6.addActionListener(this); panel.add(button_6); button_7 = new JButton("7"); button_7.setBounds(12, 146, 85, 31); button_7.addActionListener(this); panel.add(button_7); button_8 = new JButton("8"); button_8.setBounds(120, 146, 85, 31); button_8.addActionListener(this); panel.add(button_8); button_9 = new JButton("9"); button_9.setBounds(231, 146, 85, 31); button_9.addActionListener(this); panel.add(button_9); button_2 = new JButton("2"); button_2.setBounds(120, 60, 85, 31); button_2.addActionListener(this); panel.add(button_2); button_3 = new JButton("3"); button_3.setBounds(231, 60, 85, 31); button_3.addActionListener(this); panel.add(button_3); button_0 = new JButton("0"); button_0.setBounds(12, 189, 85, 31); button_0.addActionListener(this); panel.add(button_0); button_clear = new JButton("清空"); button_clear.setBounds(120, 189, 85, 31); button_clear.addActionListener(this); panel.add(button_clear); button_add = new JButton("+"); button_add.setBounds(339, 60, 85, 31); button_add.addActionListener(this); panel.add(button_add); button_minus = new JButton("-"); button_minus.setBounds(339, 103, 85, 31); button_minus.addActionListener(this); panel.add(button_minus); button_mulptily = new JButton("*"); button_mulptily.setBounds(339, 146, 85, 31); button_mulptily.addActionListener(this); panel.add(button_mulptily); button_divide = new JButton("/"); button_divide.setBounds(339, 189, 85, 31); button_divide.addActionListener(this); panel.add(button_divide); button_equal = new JButton("="); button_equal.setBounds(231, 189, 85, 31); button_equal.addActionListener(this); panel.add(button_equal); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == button_equal) { Arithmetic ari = ArithmeticFactory.getArith(textField.getText()); int num = ari.count(textField.getText()); textField.setText("" + num); } else if (e.getSource() == button_clear) { textField.setText(""); } else { textField.setText(textField.getText() + e.getActionCommand()); } }}
效果图:
阅读全文
0 0
- java使用工厂模式编写计算器(swing界面)
- java计算器(Swing绘制界面)
- 使用java swing仿window7标准计算器界面
- 简单工厂模式(计算器)(JAVA)
- JAVA 工厂模式(计算器) (修改版)
- 用Java swing 编写简单的计算器
- 计算器的Swing界面
- java swing示例(计算器)
- 用JAVA编写简易计算器界面
- java 简单工厂模式(实现一个计算器)
- 使用java swing完成简单的计算器
- java简单工厂设计模式实现计算器
- java工厂模式编写小游戏
- Swing 编写欢迎界面(引)
- Java Swing计算器
- Java-Swing-计算器
- java Swing计算器
- Java swing 计算器
- Linux如何查看JDK的安装路径
- Android studio3.0 kotlin执行main方法错误
- maven 遇到failOnMissingWebXml有关问题解决方法
- 电子合同印章大小和位置调整
- unity3d ugui text 文字颜色渐变效果~
- java使用工厂模式编写计算器(swing界面)
- 软考错题总结
- iOS 中实现绑定变化的 思路
- LDAP管理工具phpldapadmin(推荐)
- JS复习 -- 作用域链
- 1-5·Linux中帮助命令
- Python编程:从入门到实践的动手试一试答案(第七章)
- OpenCV计算机视觉编程攻略(第2版)代码
- Android显示原理简介(阿里巴巴)