简单计算器
2015-12-05 16:57
148 查看
前几天刚学的GUI,老师就布置了这个作业,我这个只实现了简单的加减乘除四则运算
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI extends JFrame { private JButton num1,num2,num3,num4,num5,num6,num7,num8,num0,num9; private JButton bt1,bt2,bt3,bt4,bt5,bt6; //private JTextField inputFild; private JTextArea chatContent1,chatContent2; private JPanel inputPanel,inputpanel1; //private JScrollPane shcrollPanel; //构造函数 public GUI() { /*******创建窗口*******/ this.setLayout(new GridLayout(2,1)); this.setTitle("计算器"); this.setSize(400, 500); this.setLocation(300, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui(); } public void gui() { chatContent1 =new JTextArea(12,34); chatContent2 =new JTextArea(4,5); chatContent1.setEditable(false); chatContent2.setEditable(true); inputpanel1=new JPanel(); inputpanel1.setLayout(new GridLayout(2,1)); inputpanel1.add(chatContent1); inputpanel1.add(chatContent2); inputPanel =new JPanel(); inputPanel.setLayout(new GridLayout(4,4)); num1=new JButton("1"); num2=new JButton("2"); num3=new JButton("3"); num4=new JButton("4"); num5=new JButton("5"); num6=new JButton("6"); num7=new JButton("7"); num8=new JButton("8"); num9=new JButton("9"); num0=new JButton("0"); bt1 =new JButton("+"); bt2 =new JButton("-"); bt3 =new JButton("*"); bt4 =new JButton("/"); bt5 =new JButton("="); bt6 =new JButton("."); inputPanel.add(num7); inputPanel.add(num8); inputPanel.add(num9); inputPanel.add(bt1); inputPanel.add(num4); inputPanel.add(num5); inputPanel.add(num6); inputPanel.add(bt2); inputPanel.add(num1); inputPanel.add(num2); inputPanel.add(num3); inputPanel.add(bt3); inputPanel.add(bt6); inputPanel.add(num0); inputPanel.add(bt5); inputPanel.add(bt4); this.add(inputpanel1); this.add(inputPanel); color();// 为按钮设置颜色和大小 Action();//为按钮添加监听器 } //为按钮添加监听器 public void Action() { num1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num1.getText()=="1") chatContent2.append(num1.getText()); } } ); num2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num2.getText()=="2") chatContent2.append(num2.getText()); } } ); num3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num3.getText()=="3") chatContent2.append(num3.getText()); } } ); num4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num4.getText()=="4") chatContent2.append(num4.getText()); } } ); num5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num5.getText()=="5") chatContent2.append(num5.getText()); } } ); num6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num6.getText()=="6") chatContent2.append(num6.getText()); } } ); num7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num7.getText()=="7") chatContent2.append(num7.getText()); } } ); num8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num8.getText()=="8") chatContent2.append(num8.getText()); } } ); num9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num9.getText()=="9") chatContent2.append(num9.getText()); } } ); num0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(num0.getText()=="0") chatContent2.append(num0.getText()); } } ); bt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(bt1.getText()=="+") chatContent2.append(bt1.getText()); } } ); bt2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(bt2.getText()=="-") chatContent2.append(bt2.getText()); } } ); bt3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(bt3.getText()=="*") chatContent2.append(bt3.getText()); } } ); bt4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(bt4.getText()=="/") chatContent2.append(bt4.getText()); } } ); bt6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(bt6.getText()==".") chatContent2.append(bt6.getText()); } } ); bt5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str=chatContent2.getText(); if(bt5.getText()=="=") chatContent2.append(bt5.getText()); double a,b,c; int num=0,num1=1; String str1,str2; for(int i=0;i<str.length();i++) { if(str.charAt(i)>='0'&&str.charAt(i)<='9'||str.charAt(i)=='.') num++; else break; } for(int j=0;j<str.length();j++) if(str.charAt(j)=='.') { num1=0; break; } str1=str.substring(0,num); str2=str.substring(num+1); a=Double.parseDouble(str1); b=Double.parseDouble(str2); if(str.charAt(num)=='+') { c=a+b; if(num1==1) { long n; n=(long)(a+b); chatContent2.append(Long.toString(n)); } else chatContent2.append(Double.toString(c)); } if(str.charAt(num)=='-') { c=a-b; if(num1==1) { long n; n=(long)(a-b); chatContent2.append(Long.toString(n)); } else chatContent2.append(Double.toString(c)); } if(str.charAt(num)=='*') { c=a*b; if(num1==1) { long n; n=(long)(a*b); chatContent2.append(Long.toString(n)); } else chatContent2.append(Double.toString(c)); } if(str.charAt(num)=='/') { c = a/b; chatContent2.append(Double.toString(c)); } chatContent1.setText(""); chatContent1.append(chatContent2.getText()); chatContent2.setText(""); } } ); } // 为按钮设置颜色和大小 public void color() { num1.setBackground(Color.white); num2.setBackground(Color.white); num3.setBackground(Color.white); num4.setBackground(Color.white); num5.setBackground(Color.white); num6.setBackground(Color.white); num7.setBackground(Color.white); num8.setBackground(Color.white); num9.setBackground(Color.white); num0.setBackground(Color.white); bt1.setBackground(Color.LIGHT_GRAY); bt2.setBackground(Color.LIGHT_GRAY); bt3.setBackground(Color.LIGHT_GRAY); bt4.setBackground(Color.LIGHT_GRAY); bt5.setBackground(Color.white); bt6.setBackground(Color.white); bt5.setFont(new Font("黑体",Font.BOLD,40)); } public static void main(String[] args) { new GUI(); } }
相关文章推荐
- Android Stuido 实现多渠道打包
- Android笔记-Android启动之Linux内核启动
- I/O设备管理
- python基本 迭代
- Spring源码阅读3-bean获取-上
- 线程机制
- C# ComBox 垂直滚动条
- 15000个英文单词Sec01.Part02数字与度量衡
- MySQL约束
- JDBC基础
- Linux系统之TroubleShooting
- 【saltstack学习笔记之八】salt的sls文件
- 微信公众号的黑色商业链揭秘
- Spring mvc 框架下实现页面过期时,登录后返回原页面
- (java)Plus One
- 51nod 1004 n^n的末尾数字
- H5中需要掌握的 ANIMATION 动画效果
- 新手用eclipse运行web工程时经常会遇到的一个问题
- Linux下MySQL安装
- python基础教程共60课-第6课bool