登录界面进入计算器

这是3月中旬写的计算器,通过与之前刚刚学习的登录界面相结合,通过登录界面进入计算器。

首先,谈一谈感想,对于计算器模块,基本上都是自己在冥思苦想,花了一天弄出来的,真心不容易啊,界面还是相当好看的。

计算器的button很多,当时采用的是空布局,对每个button的位置和内容是一个个设置的,十分的繁琐。而这可以通过用面板来实现,比如数字键盘构造一个网格布局的面板。

而且,计算器只实现了加减乘除,取余,倒数,取反,根号这些功能,对于实数只能输出数字,而不能进行运算。并且,整数的运算第二个数只能是个位数,当然,这个可以用将运算符及前面的数清空来实现。

//登录界面 (仿QQ界面)
import java.awt.Color;
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 {
		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);

			LoginListener m = new LoginListener (name,password);
			loginbutton.addActionListener(m);

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

 

//登录界面的监听器,计算器的入口
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 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, "账号或者密码错误");
	}

	

}

 

 

//计算器的监听器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class ButtonListener implements ActionListener  {
	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);
		}
	}



}

 


 

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值