第二次作业

发布时间 2023-11-15 17:22:10作者: 礼礼耶

一、代码

登录界面

package jisuanqi;
import java.awt.Color;
import java.awt.event.ContainerListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame implements ContainerListener {
    public LoginFrame() {
        // TODO Auto-generated constructor stub
    }
    public LoginFrame(JTextField name, JPasswordField password) {
        // TODO Auto-generated constructor stub
    }
    public void last(){
        //创建窗体
        JFrame jf = new JFrame();
        //设置边框
        jf.setTitle("用户!");
        jf.setDefaultCloseOperation(3);
        
        jf.setLayout(null);
        
        //设置窗体颜色
        jf.setBackground(Color.BLUE);
    
        //设置窗体位置及大小
        jf.setBounds(490,240,380,290);
        jf.setResizable(false);//设置禁止用户调整窗体的大小

        JLabel label1 = new JLabel("帐号");
        jf.add(label1);
        label1.setBounds(110,120,30,22);    

        JTextField  name= new JTextField ();
        jf.add(name);    
        name.setBounds(150,120,150,22);
        
        JLabel label2 = new JLabel("密码");
        jf.add(label2);
        label2.setBounds(110,150,30,22);
        
        JPasswordField password= new JPasswordField(10);
        jf.add(password);
        password.setBounds(150,150,150,22);
    
        JButton loginbutton = new JButton("登录");
        jf.add(loginbutton);
        loginbutton.setBounds(110,215,150,30);
        
        JCheckBox box = new JCheckBox("记住密码");
        jf.add(box);
        box.setBounds(110,180,80,15);
    
        JCheckBox box1= new JCheckBox("自动登录");
        jf.add(box1);
        box1.setBounds(200,180,80,15);

        ImageIcon icon1 = new ImageIcon("img/qizai.jpg");
        JLabel imgLabel2 = new JLabel(icon1);
        imgLabel2.setBounds(20,120,80,80);
        jf.add(imgLabel2);
        jf.setVisible(true);
        
        ImageIcon icon = new ImageIcon("img/天空.jpg");
        JLabel imgLabel = new JLabel(icon);
        imgLabel.setBounds(0,0,400,300);
            jf.add(imgLabel);

            ContainerListener m = new LoginFrame (name,password);
        loginbutton.addContainerListener(m);

        jf.setVisible(true);
    }
    public static void main(String[] args) {
        LoginFrame f1 = new LoginFrame();
        f1.last();
        }

  }

登录界面的监听器

package jsq;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginListener{
    public class LoginListener implements ActionListener {
        private JTextField name ;
        private JPasswordField password;
        public LoginListener (JTextField name,JPasswordField password){
            this.name=name;
            this.password=password;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String nm=name.getText();
            String ps=password.getText();
            if((nm.equals("5") && ps.equals("1")) || (nm.equals("123456789") && ps.equals("12345"))){
                JFrame jf1= new JFrame();
                jf1.setTitle("英雄联盟计算器!");        //设置边框
                jf1.setDefaultCloseOperation(3);
                jf1.setLayout(null);                    //设置窗体的布局方式为空布局        
                jf1.setBackground(Color.GRAY);            //设置窗体颜色
                jf1.setBounds(480,150,370,450);            //设置窗体位置及大小
                jf1.setResizable(false);                //设置禁止用户调整窗体的大小
    
    
                JTextField  field= new JTextField (15);
                jf1.add(field);    
                field.setBounds(10,10,340,150);
                field.setEditable(false);
                field.setOpaque(false);
    
                JButton b21= new JButton("←");
                jf1.add(b21);
                b21.setBounds(10,170,60,40);
            
                ButtonListener m21 = new ButtonListener(field);
                b21.addActionListener(m21);
                jf1.setVisible(true);
                
                JButton b20= new JButton("CE");
                jf1.add(b20);
                b20.addActionListener(m21);
                b20.setBounds(80,170,60,40);
                
                JButton b19= new JButton("C");
                jf1.add(b19);
                b19.addActionListener(m21);
                b19.setBounds(150,170,60,40);
                
                JButton b18= new JButton("±");
                jf1.add(b18);
                b18.addActionListener(m21);
                b18.setBounds(220,170,60,40);
                
                JButton b17= new JButton("√");
                jf1.add(b17);
                b17.addActionListener(m21);
                b17.setBounds(290,170,60,40);
    
                JButton b7= new JButton("7");
                jf1.add(b7);
                b7.addActionListener(m21);
                b7.setBounds(10,220,60,40);
                
                JButton b8= new JButton("8");
                jf1.add(b8);
                b8.addActionListener(m21);
                b8.setBounds(80,220,60,40);
                
                JButton b9= new JButton("9");
                jf1.add(b9);
                b9.addActionListener(m21);
                b9.setBounds(150,220,60,40);
            
                JButton b10= new JButton("/");
                jf1.add(b10);
                b10.addActionListener(m21);
                b10.setBounds(220,220,60,40);
                
                JButton b16= new JButton("%");
                jf1.add(b16);
                b16.addActionListener(m21);
                b16.setBounds(290,220,60,40);
                
                JButton b4= new JButton("4");
                jf1.add(b4);
                b4.addActionListener(m21);
                b4.setBounds(10,270,60,40);
                
                JButton b5= new JButton("5");
                jf1.add(b5);
                b5.addActionListener(m21);
                b5.setBounds(80,270,60,40);
                
                JButton b6= new JButton("6");
                jf1.add(b6);
                b6.addActionListener(m21);
                b6.setBounds(150,270,60,40);
                
                JButton b11= new JButton("*");
                jf1.add(b11);
                b11.addActionListener(m21);
                b11.setBounds(220,270,60,40);
                
                JButton b15= new JButton("1/x");
                jf1.add(b15);
                b15.addActionListener(m21);
                b15.setBounds(290,270,60,40);
                
                JButton b1= new JButton("1");
                jf1.add(b1);
                b1.addActionListener(m21);
                b1.setBounds(10,320,60,40);
                
                JButton b2= new JButton("2");
                jf1.add(b2);
                b2.addActionListener(m21);
                b2.setBounds(80,320,60,40);
                
                JButton b3= new JButton("3");
                jf1.add(b3);
                b3.addActionListener(m21);
                b3.setBounds(150,320,60,40);
                
                JButton b12= new JButton("-");
                jf1.add(b12);
                b12.addActionListener(m21);
                b12.setBounds(220,320,60,40);
            
                JButton b14= new JButton("=");
                jf1.add(b14);
                b14.addActionListener(m21);
                b14.setBounds(290,320,60,90);
                
                JButton b0= new JButton("0");
                jf1.add(b0);
                b0.addActionListener(m21);
                b0.setBounds(10,370,130,40);
    
                JButton b23= new JButton(".");
                jf1.add(b23);
                b23.addActionListener(m21);
                b23.setBounds(150,370,60,40);
                
                JButton b13= new JButton("+");
                jf1.add(b13);
                b13.addActionListener(m21);
                b13.setBounds(220,370,60,40);
    
                ImageIcon icon3 = new ImageIcon("img/blue.jpg");
                JLabel imgLabel3= new JLabel(icon3);
                imgLabel3.setBounds(0,0,400,420);
                 jf1.add(imgLabel3);
                 
    
                 
                jf1.setVisible(true);
                
            }
            else JOptionPane.showMessageDialog(null, "账号或者密码错误");
        }
    
        
     }
}

 
计算器的监听器

package jsqy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class ButtonListener {
    private String content;
    int number;
    double re,ff;
    int a=0,b,z=0;
    double rec,sq,poi=0;
    String m,p="",mi="",mul="",div="",rem="",po="";
    double neg;
    
    private JTextField jt;
 
 
    //定义运算函数
    double plus ( double m, double n ) {
        return m+n;
    }
    double minus(double m ,double n) {
        return m-n;
    }
    double multiply(double m,double n) {
        return m*n;
    }    
    double divide(double m,double n) {
        return m/n;
    }
    int remainder (int  m,int  n) {
        return m%n;
    }
    double reciproc (double m) {
        return (double) 1/m;
    }
    double negate(double m) {
        m=-m;
        return m;
    }

public ButtonListener(JTextField jt){
        this.jt=jt;
    }
    
    public void actionPerformed(ActionEvent e) {
        
        content=jt.getText();  //获取输入框中的文本
        
        try{
            //ff = Float.parseFloat(e.getActionCommand());
            number=Integer.parseInt(e.getActionCommand());    //将事件源转化为数字进行判断
        }catch(Exception ex){
    }
        if(0<=number && number<10){            //判断事件源为数字
            b=number;
            
            if(content.equals(p)){            //通过判断选择与之对应的函数进行运算
                re=plus(a,b);                //加法运算
            }
            if(content.equals(mi)){
                re=minus(a,b);                //减法运算
            }
            if(content.equals(mul)){
                re=multiply(a,number);            //乘法运算
            }                
            if(content.equals(div)){
                re=divide(a,number);                //除法运算
            }
            if(content.equals(rem)){
                re=remainder(a,number);            //取余运算
            }
            if(content.equals(po)){
                
                jt.setText(""+poi);            //小数点运算
            }
            jt.setText(content+number);    
        }
        
        if(e.getActionCommand().equals("+")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            p=content+m;
            
        }

else if(e.getActionCommand().equals("-")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            mi=content+m;
        }
        else if(e.getActionCommand().equals("*")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            mul=content+m;
        }
        else if(e.getActionCommand().equals("/")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            div=content+m;
        }
        else if(e.getActionCommand().equals("%")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            rem=content+m;
        }
        else if(e.getActionCommand().equals("1/x")){
            a=Integer.parseInt(content);
            rec=reciproc(a);
            jt.setText("reciproc("+content+")="+rec);
        }
        else if(e.getActionCommand().equals("±")){
            a=Integer.parseInt(content);
            neg=negate(a);
            jt.setText("reciproc("+content+")="+neg);
        }
        else if(e.getActionCommand().equals("√")){
            a=Integer.parseInt(content);
            sq=Math.sqrt(a);
            jt.setText("sqrt("+content+")="+sq);
        }    
        else if(e.getActionCommand().equals(".")){            
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            po=content+m;
            jt.setText(po);
       }
                //以下还未实现
        else if(e.getActionCommand().equals("C")){
            jt.setText(content+m);
        }
        else if(e.getActionCommand().equals("CE")){
            jt.setText(content+m);
        }
        else if(e.getActionCommand().equals("←")){
            jt.setText(content+e.getActionCommand());
            m=e.getActionCommand();
            a=Integer.parseInt(content);
            jt.setText(content+m);
            mi=content+m;
        }
        else if(e.getActionCommand().equals("=")){
            jt.setText(content+e.getActionCommand()+re);
        }
    }
}

 

 二、计算过程程序流程图