Java的事件处理机制

【实验十八】

1.分别实现加法器、计算器的功能。
加法器源程序:

package chapter01;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Jfq implements ActionListener{
	JButton b1;
	JLabel l1,l2,la;
	JTextField t1,t2,t3;
	JFrame f;
	public Jfq() {
		f = new JFrame();
		l1 = new JLabel("操作数1",JLabel.CENTER);
		l2 = new JLabel("操作数2",JLabel.CENTER);
		la = new JLabel("0.0");
		l1.setBorder(BorderFactory.createEtchedBorder());
		l2.setBorder(BorderFactory.createEtchedBorder());
		t1 = new JTextField();
		t2 = new JTextField();
		b1 = new JButton("求和");
		b1.addActionListener(this);
		f.add(l1);
		f.add(t1);
		f.add(l2);
		f.add(t2);
		f.add(b1);
		f.add(la);
		f.setBounds(400, 400, 400, 400);
		f.setVisible(true);
		f.setLayout(new GridLayout(3,2));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String s1 = t1.getText();
		String s2 = t2.getText();
		double d1 = Double.parseDouble(s1);
		double d2 = Double.parseDouble(s2);
		double sum = d1+d2;
		la.setText(sum+"");
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Jfq();
	}

}

计算器源程序:

package chapter01;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Jsq implements ActionListener{
	JPanel p1,p2,p3,p4;
	JLabel l1,l2;
	JButton ba,bb,bc,bd;
	JTextField t1,t2,t3;
	JFrame f;
	Jsq(){
		f = new JFrame();
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		l1 =new JLabel("操作数1");
		l1.setOpaque(true);
		l1.setBackground(Color.green);
		l1.setForeground(Color.red);
		l2 =new JLabel("操作数2");
		l2.setOpaque(true);
		l2.setBackground(Color.magenta);
		l2.setForeground(Color.red);
		ba =new JButton("相加");
		bb =new JButton("相减");
		bc =new JButton("相乘");
		bd =new JButton("相除");
		//给四个按钮添加监听器
		ba.addActionListener(this);
		bb.addActionListener(this);
		bc.addActionListener(this);
		bd.addActionListener(this);
		ba.setBackground(Color.lightGray);
		bb.setBackground(Color.lightGray);
		bc.setBackground(Color.lightGray);
		bd.setBackground(Color.lightGray);
		t1 = new JTextField(12);
		t2 = new JTextField(12);
		t3 = new JTextField(12);
		t3.setBackground(Color.lightGray);
		p1.add(l1);
		p1.add(t1);
		p2.add(l2);
		p2.add(t2);
		p3.add(ba);
		p3.add(bb);
		p3.add(bc);
		p3.add(bd);
		p4.add(t3);
		f.add(p1);
		f.add(p2);
		f.add(p3);
		f.add(p4);
		
		f.setBounds(400, 400, 400, 400);
		f.setVisible(true);
		f.setLayout(new GridLayout(4,1));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String s1 = t1.getText();
		String s2 = t2.getText();
		double d1 = Double.parseDouble(s1);
		double d2 = Double.parseDouble(s2);
		double sum = 0;
		//如果点“相加”,e。getSource()是看监听器被发动
		if(e.getSource()==ba)
			sum = d1+d2;
		else if (e.getSource()==bb)
			sum = d1-d2;
		else if(e.getSource()==bc)
			sum = d1*d2;
		else
			sum = d1/d2;
		t3.setText(sum+" ");
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Jsq();
	}

}

2、实现用户登录的模拟功能,假设合法用户名:abc,密码:123。

待修改,功能不完善,页面太丑

package chapter01;

import java.awt.Dialog;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class dl implements ActionListener{
	JFrame j;
	JPanel p1,p2,p3;
	JLabel l1,l2;
	JButton b1,b2;
	JTextField t1;
	JPasswordField p;
	dl(){
		j = new JFrame("登录窗口");
		p1=new JPanel();
		p2=new JPanel();
		p3=new JPanel();
		l1=new JLabel("用户名");
		t1=new JTextField(12);
		l2=new JLabel("密    码");
		p=new JPasswordField(12);
		b1=new JButton("确定");
		b2=new JButton("取消");
		b1.addActionListener(this);
		b2.addActionListener(this);
		p1.add(l1);
		p1.add(t1);
		p2.add(l2);
		p2.add(p);
		p3.add(b1);
		p3.add(b2);
		j.add(p1);
		j.add(p2);
		j.add(p3);
		j.setBounds(500, 500, 300, 300);
		j.setVisible(true);
		j.setLayout(new GridLayout(3, 1));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JDialog d1 = new JDialog();
		d1.setBounds(500, 500, 200, 200);
		String s1,s2,yhm,mm;
		s1 = t1.getText();
		s2 = p.getText();
		yhm = "abc";
		mm = "123";
		if(s1.equals(yhm)&&s2.equals(mm)) 
			JOptionPane.showMessageDialog(null, "登录成功");
		else if(s1.equals(yhm)&&s2.equals(mm)==false)
			JOptionPane.showMessageDialog(null, "密码错误");
		else if(s1.equals(yhm)==false&&s2.equals(mm)) 
			JOptionPane.showMessageDialog(null, "用户名错误");
		//如果点“取消”关闭窗口
		if(e.getSource()==b2) 
			System.exit(0);	
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new dl();
	}

}

3.实现电子相册

package chapter01;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class xc implements ActionListener{
	JFrame f;
	JPanel p1,p2;
	JButton b1,b2,b3,b4;
	JLabel l1;
	ImageIcon i1,i2,i3,i4;
	xc(){
		f = new JFrame();
		p1 = new JPanel();
		p2 = new JPanel();
		b1 = new JButton("湘北");
		b2 = new JButton("海南");
		b3 = new JButton("陵南");
		b4 = new JButton("翔阳");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		i1 = new ImageIcon("img\\xb.jpg");
		i2 = new ImageIcon("img\\hn.jpg");
		i3 = new ImageIcon("img\\ln.jpg");
		i4 = new ImageIcon("img\\xy.png");
		l1 = new JLabel(i1);
		f.setLayout(new BorderLayout());
		p1.add(b1);
		p1.add(b2);
		p1.add(b3);
		p1.add(b4);
		p2.add(l1);
		f.add(p1,BorderLayout.NORTH);
		f.add(p2,BorderLayout.CENTER);
		
		f.setBounds(500, 500, 700, 700);
		f.setVisible(true);
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==b1)
			l1.setIcon(i1);
		else if(e.getSource()==b2)
			l1.setIcon(i2);
		else if(e.getSource()==b3)
			l1.setIcon(i3);
		else if(e.getSource()==b4)
			l1.setIcon(i4);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new xc();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值