java学习的第一个成果 一 简易的计算器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZCS_001/article/details/78670179

首先

一下我编写简易计算器用到的相关技术(狗屁技术,就这个你自己也好意思叫技术微笑,但是还是要说的,给自己以后回头来看)。

1.swing;用swing来编写一个窗口 ,然后创
建一个内容,这个是很简单的。其中难到我的是监视器。
给按钮注册监视器,当按钮被点击,会传一个对象给 public void actionPerformed(ActionEvent e),就是这个方法里面的实现,有点小麻烦,
最后解决之后也觉得很简单。
(ps:现在毕竟技术有限,我自己知道这个计算器还是有很多的bug,我自己随便弄弄就坏了。但是要抓住 主—次 矛盾,等我更进一步的话,回头再来改也来得及,把主要精力花在该花的地方,不要钻牛角尖大笑,其实就是自己懒加上技术不精湛,不会改,嘿嘿。)
2.接下来附上源码和成果截图。
3.我自己加油!!嗯!加油!

import java.awt.*;


import javax.swing.*;


public class MyCalculator extends JFrame {
JMenuBar menuBar;
JMenu menu1,menu2,menu3;
JMenuItem item1;
JTextField text;
JButton button1,button2,button3,button4;
JPanel panel;
public MyCalculator(String s,int x,int y,int w,int h) {
init();
setTitle(s);
setBounds(x,y,w,h);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void init() {
text = new JTextField(240);
add(text,BorderLayout.NORTH);
panel = new JPanel();
panel.setLayout(new GridLayout(4,4));
button1 = new JButton("1");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("2");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("3");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button2 = new JButton("+");
button2.addActionListener(new MyMonitor(this));
panel.add(button2);
button1 = new JButton("4");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("5");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("6");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button2 = new JButton("-");
button2.addActionListener(new MyMonitor(this));
panel.add(button2);
button1 = new JButton("7");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("8");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button1 = new JButton("9");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button2 = new JButton("*");
button2.addActionListener(new MyMonitor(this));
panel.add(button2);
button1 = new JButton("0");
button1.addActionListener(new MyMonitor(this));
panel.add(button1);
button2 = new JButton("/");
button2.addActionListener(new MyMonitor(this));
panel.add(button2);
button2 = new JButton("%");
button2.addActionListener(new MyMonitor(this));
panel.add(button2);
button3 = new JButton("=");
button3.addActionListener(new MyMonitor(this));
panel.add(button3);
add(panel,BorderLayout.CENTER);
button4 = new JButton("CE");
button4.addActionListener(new MyMonitor(this));
add(button4,BorderLayout.WEST);
init1();
}


void init1() {
menuBar = new JMenuBar();
menu1 = new JMenu("编辑");
menu2 = new JMenu("查看");
menu3= new JMenu("作者");
item1= new JMenuItem("赵春生");
menu3.add(item1);
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
setJMenuBar(menuBar);
}


}



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MyMonitor implements ActionListener {
MyCalculator mc;
static double num1,num2;
static int x=0;
String s;
public MyMonitor(MyCalculator mc) {
super();
this.mc = mc;
}






public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("+")) {
s = mc.text.getText();  
            num1 = Double.parseDouble(s);  
            mc.text.setText("");  
            x=1;  
}
else if(e.getActionCommand().equals("-")) {
s = mc.text.getText();  
            num1 = Double.parseDouble(s);  
            mc.text.setText("");  
            x=2;  
}
else if(e.getActionCommand().equals("*")) {
s = mc.text.getText();  
            num1 = Double.parseDouble(s);  
            mc.text.setText("");  
            x=3;  
}
else if(e.getActionCommand().equals("/")) {
s = mc.text.getText();  
            num1 = Double.parseDouble(s);  
            mc.text.setText("");  
            x=4;  
}
else if(e.getActionCommand().equals("%")) {
s = mc.text.getText();  
            num1 = Double.parseDouble(s);  
            mc.text.setText("");  
            x=5;  
}
else if(e.getActionCommand().equals("="))  {  
            s =mc.text.getText();  
            num2 = Double.parseDouble(s);  
            switch(x)  
            { 
            case  0 : mc.text.setText("不要瞎鸡巴乱搞,要计算就计算,计算器要被你玩坏了");break; 
            case  1 : mc.text.setText(" " + (num1 + num2));break;  
            case  2 : mc.text.setText(" " + (num1 - num2));break;  
            case  3 : mc.text.setText(" " + (num1 * num2));break; 
            case  4 : mc.text.setText(" " + (num1 / num2));break; 
            case  5 : mc.text.setText(" " + (num1 % num2));break;  
            }  
             
        } 
else if(e.getActionCommand().equals("CE")) {
mc.text.setText("");
}
else  {  
            String s = mc.text.getText();  
            s = s + e.getActionCommand();  
            mc.text.setText(s);  
        }  
}


}


public class TestCalculator {


public static void main(String[] args)  {
new MyCalculator("简易的计算器",300,300,300,300);


}


}




展开阅读全文

没有更多推荐了,返回首页