자바 의 초등학생 사 칙 연산

13305 단어
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

좋은 웹페이지 즐겨찾기