• java swing 简易flowLayout 计算器 带功能实现 支持小数


    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    class sss implements ActionListener{
       JTextField text1,text2,text3;
       JButton button;
       ActionListener bl;
       
       JComboBox box;
       double number1,number2;
        sss(){
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());  
            // 等价于frame.getContentPane().setLayout(new FlowLayout());
            text1 = new JTextField(8);
        	text2 = new JTextField(8);
        	text3 = new JTextField(8);
        	button =  new JButton("=");
            
            String[] arr = new String[] {"+","-","*","/"} ;
            box =  new JComboBox();  
            for(int i = 0; i<arr.length;i++) {
           	box.addItem(arr[i]);  
            }
            button.addActionListener(this);
            text1.addActionListener(this);
            text2.addActionListener(this);
            text3.addActionListener(this);
            
            frame.add(text1);   
            frame.add(box);  
            frame.add(text2);  
            frame.add(button);  
            frame.add(text3);  
            
            frame.pack();
            frame.setVisible(true);
     
       
        }
        
        public static void main(String[] args) {        
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new sss();
                }
            });
        }
       
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    		String str1 = text1.getText();
    		number1 = Double.valueOf(str1);
    		String str2 = text2.getText();
    		number2 = Double.valueOf(str2);
    		String fuhao = (String) box.getSelectedItem();
    		
    		if(e.getSource().equals(button)){
    			if(fuhao == "+") {
    				double result  = number1 + number2;
    	             text3.setText(""+result);
    			}
    			 
    			if(fuhao == "-") {
    				double result  = number1 - number2;
    	             text3.setText(""+result);
    			}
    			
    			if(fuhao == "*") {
    				double result  = number1 * number2;
    	             text3.setText(""+result);
    			}
    			
    			if(fuhao == "/") {
    				double result  = number1 / number2;
    	             text3.setText(""+result);
    			}
    		}
    		
    		
    		
    	}
    }
    
    



    鲜花会生锈,盐巴会腐烂
  • 相关阅读:
    【JVM基础】JVM垃圾回收机制算法
    【java基础】- java双亲委派机制
    Java基础(一)
    JVM
    冷知识: 不会出现OutOfMemoryError的内存区域
    疯狂Java:突破程序员基本功的16课-李刚编著 学习笔记(未完待续)
    nor flash之写保护
    spinor/spinand flash之高频通信延迟采样
    nor flash之频率限制
    使用littlefs-fuse在PC端调试littlefs文件系统
  • 原文地址:https://www.cnblogs.com/hunterxing/p/9709303.html
Copyright © 2020-2023  润新知