zoukankan      html  css  js  c++  java
  • 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);
    			}
    		}
    		
    		
    		
    	}
    }
    
    



    鲜花会生锈,盐巴会腐烂
  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/hunterxing/p/9709303.html
Copyright © 2011-2022 走看看