zoukankan      html  css  js  c++  java
  • 简易计算机

    package 考试题目_简易计算器;
    	import java.awt.*;
    	import javax.swing.*;
    	import java.awt.event.*;
    	public class Jisuanqi extends JFrame implements ActionListener{
    		JTextField text1, text2, text3;
    		JRadioButton btnAdd, btnSub, btnMul, btnDiv;
    		ButtonGroup bg;
    		JLabel num1, num2, result;
    		JButton solve;
    		Box base, boxH1, boxH2, boxH3, boxH4;
    		
    		public Jisuanqi(String name){
    			super(name);
    			setLayout(new FlowLayout());
    			text1 = new JTextField(10);
    			text2 = new JTextField(10);
    			text3 = new JTextField(10);
    			btnAdd = new JRadioButton("+");
    			btnSub = new JRadioButton("-");
    			btnMul = new JRadioButton("*");
    			btnDiv = new JRadioButton("/");
    			num1 = new JLabel("数一");
    			num2 = new JLabel("数二");
    			result = new JLabel("结果");
    			solve = new JButton("=");
    			solve.addActionListener(this);
    			bg = new ButtonGroup();
    			bg.add(btnAdd);
    			bg.add(btnSub);
    			bg.add(btnMul);
    			bg.add(btnDiv);
    					
    			boxH1 = Box.createHorizontalBox();
    			boxH1.add(num1);
    			boxH1.add(Box.createHorizontalStrut(30));
    			boxH1.add(text1);
    			boxH2 = Box.createHorizontalBox();
    			boxH2.add(btnAdd);
    			boxH2.add(btnSub);
    			boxH2.add(btnMul);
    			boxH2.add(btnDiv);
    			boxH3 = Box.createHorizontalBox();
    			boxH3.add(num2);
    			boxH3.add(Box.createHorizontalStrut(30));
    			boxH3.add(text2);
    			boxH4 = Box.createHorizontalBox();
    			boxH4.add(result);
    			boxH4.add(Box.createHorizontalStrut(30));
    			boxH4.add(text3);
    			base = Box.createVerticalBox();
    			base.add(boxH1);
    			base.add(Box.createVerticalStrut(25));
    			base.add(boxH2);
    			base.add(Box.createVerticalStrut(25));
    			base.add(boxH3);
    			base.add(Box.createVerticalStrut(25));
    			base.add(solve);
    			base.add(Box.createVerticalStrut(25));
    			base.add(boxH4);
    			add(base);
    			
    			setSize(300, 320);
    			setVisible(true);
    			validate();
    			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		}
    		
    		public void actionPerformed(ActionEvent e){
    			double n = 0;
    			double n1,n2;
    			try{
    					n1=Double.parseDouble(text1.getText());
    					n2=Double.parseDouble(text2.getText());
    					if(btnAdd.isSelected())
    						n = n1+n2;
    					else if(btnSub.isSelected())
    						n = n1-n2;
    					else if(btnMul.isSelected())
    						n = n1*n2;
    					else if(btnDiv.isSelected())
    						n = n1/n2;
    					text3.setText(String.valueOf(n));
    					
    			}catch(NumberFormatException nfe){
    					text3.setText("请输入数字字符");
    			}
    			
    		}
    		
    		public static void main(String args[]){
    			new Jisuanqi("简易计算器");
    		}
    
    	}
    

      

  • 相关阅读:
    shell脚本中的进度指示器
    shell脚本输出带颜色字体
    Linux awk命令用法
    Kubernetes系列02—Kubernetes设计架构和设计理念
    kubernetes学习01—kubernetes介绍
    kubernetes 06—kubernetes资源清单定义
    http服务详解 一次完整的请求过程
    Mysql数据库的二进制安装和基础入门操作
    项目实战10.1—企业级自动化运维工具应用实战-ansible
    linux OSI七层模型、TCP/IP协议栈及每层结构
  • 原文地址:https://www.cnblogs.com/spsglz/p/8137570.html
Copyright © 2011-2022 走看看