java的小学生四则运算

import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
public class Login extends JFrame  {
    private TextField f1;
    private TextField f2;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    Yunsuan scs=new Yunsuan();
    //登陆界面
    public Login(){
        Container cp=getContentPane();//容器
        cp.setLayout(new GridLayout(3,1));//三行一列布局
        Label l1=new Label("用户名");
        Label l2=new Label("密   码");
        Panel p1=new Panel();
        Panel p2=new Panel();
        Panel p3=new Panel();
        f1=new TextField(10);
        f2=new TextField(10);
        f2.setEchoChar('*');//回显字符为*
        b1=new JButton("登录");
        b2=new JButton("重置");
        b3=new JButton("退出");
        p1.add(l1);//第一行添加label 1
        p1.add(f1);
        p2.add(l2);
        p2.add(f2);
        p3.add(b1);
        p3.add(b2);
        p3.add(b3);
        cp.add(p1);
        cp.add(p2);
        cp.add(p3);
        b1.addActionListener(new Enter());
        b2.addActionListener(new ReWrite());
        b3.addActionListener(new Close());
        }
    class Enter implements ActionListener{
        public void actionPerformed(ActionEvent e)
        {      
                 if((f1.getText()).equals("czp")&&(f2.getText()).equals("123456"))
                 {        
                         
                         Yunsuan  frame1 = new Yunsuan();
                         frame1.setBounds(200, 200, 300, 300);
                         frame1.setVisible(true);
                 }
                 else JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新登录!");
        }
    }
    class ReWrite implements ActionListener{
        public void actionPerformed(ActionEvent e)
        {
            f1.setText("");
            f2.setText("");
            f1.requestFocus();
        }
    }
    class Close implements ActionListener{
        public void actionPerformed(ActionEvent e)
        {
            JButton bt=(JButton)e.getSource();
            if(bt==b3){
                System.exit(0);
            }
        }
}
//主函数 程序开始
    public static void main(String[] args) {
        Login log=new Login();
        log.setTitle("系统登录");
        log.setBounds(200, 200, 300, 300);
        log.setBackground(Color.blue);
        log.setVisible(true);
         new Yunsuan();
         
    }
}

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Yunsuan extends JFrame {
    private  JTextField textNum1;
    private  JTextField textNum2;
    private  JTextField result;
    private  JTextField result1;
    private  JComboBox opers;
     
    public Yunsuan() {
        init();
        setSize(300,100);
        setLocationRelativeTo(null);
        setTitle("小学生运算");
        setDefaultCloseOperation(3);
        setVisible(false);
    }
     
    private void init(){
        String[] operators = "+,-,*,/".split(",");
        textNum1 = new JTextField();
        textNum1.setColumns(10);
        textNum2 = new JTextField();
        textNum2.setColumns(10);
        result = new JTextField();
        result.setColumns(10);
        result1= new JTextField();
        result1.setColumns(10);
        opers = new JComboBox(operators);
        opers.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int operator = opers.getSelectedIndex();
                double num1 = Double.parseDouble(textNum1.getText());
                double num2 = Double.parseDouble(textNum2.getText());
             switch(operator){
                case 0:result1.setText(num1 + num2+"");break;
                case 1:result1.setText(num1 - num2+"");break;
                case 2:result1.setText(num1 * num2+"");break;
                case 3:result1.setText(num1 / num2+"");break;
                }
                 
            }
           
        });
    
      
        
        JPanel p = new JPanel();
        p.add(textNum1);
        p.add(textNum2);
        p.add(opers);
        p.add(result);
        p.add(result1);
        this.add(p);
     
}
}

 

转载于:https://www.cnblogs.com/42chenzhipeng/p/4411067.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值