23.2监听器

package study;

import java.awt.Font;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//单个监听器
public class Exercise1 extends JFrame implements ActionListener{
	JPasswordField p;
	JTextField t;
//为了再监听方法中能用到该值,所以将其定义为属性
	public Exercise1() {
		setBounds(1000, 100, 400, 200);
		Font f1 = new Font("隶书", Font.BOLD+Font.BOLD, 20);
		setLayout(new GridLayout(3,1));
		JPanel p1 = new JPanel();
		JLabel l1 = new JLabel("账号");
		l1.setFont(f1);
		t = new JTextField(20);
		p1.add(l1);
		p1.add(t);
		
		JPanel p2 = new JPanel();
		JLabel l2 = new JLabel("密码");
		l2.setFont(f1);
		p =new JPasswordField(20);
		p2.add(l2);
		p2.add(p);
		
		JPanel p3 = new JPanel();
		JButton b = new JButton("登陆");
		b.addActionListener(this);
		p3.add(b);
		
		add(p1);
		add(p2);
		add(p3);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new Exercise1();

	}


	public void actionPerformed(ActionEvent arg0) {
		if(t.getText().equals("")||p.getText().equals("")||t.getText().equals(null)||p.getText().equals(null)) {
			JOptionPane.showMessageDialog(null, "输入不能为空");
		}else {
			System.out.println("输入正确");
			System.out.println("账号:"+t.getText());
			System.out.println("密码:"+p.getText());
		}
		
	}

}

package study;

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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//多个监听器时如何区分每个监听器
public class Exercise2 extends JFrame implements ActionListener{
	JTextField t;
	JPasswordField p;
	JButton b1,b2;
	public Exercise2() {
		setBounds(1000, 100, 400, 200);
		setLayout(new GridLayout(3, 1));
		
		JPanel p1 = new JPanel();
		JLabel l1 = new JLabel("账号");
		t = new JTextField(20);
		p1.add(l1);
		p1.add(t);
		
		JPanel p2 = new JPanel();
		JLabel l2 = new JLabel("密码");
		p = new JPasswordField(20);
		p2.add(l2);
		p2.add(p);
		
		JPanel p3 = new JPanel();
		b1 = new JButton("登陆");
		b2 = new JButton("注册");
		b1.addActionListener(this);
		b2.addActionListener(this);
		p3.add(b1);
		p3.add(b2);
		
		add(p1);
		add(p2);
		add(p3);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new Exercise2();

	}


	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1) {
			if(t.getText().equals("")||p.getText().equals("")||t.getText().equals(null)||p.getText().equals(null)) {
				JOptionPane.showMessageDialog(null, "输入为空");
			}else {
				JOptionPane.showMessageDialog(null, "登陆成功");
				System.out.println("账号"+t.getText());
				System.out.println("密码"+p.getText());
				System.out.println("-------------------------------");
			}
		}else if(e.getSource()==b2) {
			JOptionPane.showMessageDialog(null, "注册成功");
		}
		
	}

}

package study;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//点击切换颜色
public class Exercise3 extends JFrame implements ActionListener{
	JLabel l2;	
	JButton b1,b2;
	public Exercise3() throws Exception {
		super();
		setSize(400, 400);
		setLocation(800, 50);
		setResizable(false);
		JPanel p1=new JPanel(new BorderLayout());
		ImageIcon img1=new ImageIcon("E:\\java的工作空间\\21.2图形化界面--监听器\\src\\study\\1.JPG");
		JLabel l1=new JLabel(img1);
		l2=new JLabel("同一个世界,同一个梦想",JLabel.CENTER);
		Font f1=new Font("隶书", Font.BOLD, 30);
		l2.setFont(f1);
		l2.setForeground(Color.blue);
		img1.setImage(img1.getImage().getScaledInstance(250, 250, Image.SCALE_DEFAULT));
		p1.add(l1,"Center");p1.add(l2,"South");
		add(p1,"Center");
		JPanel p2=new JPanel();
		b1=new JButton("换颜色");
		b2=new JButton("退出");
		b1.addActionListener(this);
		b2.addActionListener(this);
		p2.add(b1);p2.add(b2);
		add(p2,"South");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1){
			l2.setForeground(new Color(new Random().nextInt(255),new Random().nextInt(255),new Random().nextInt(255)));
		}else if(e.getSource()==b2){
			System.exit(0);
		}
		
	}

	public static void main(String[] args) throws Exception {
		new Exercise3();
	}
}

package study;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
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 Exercise4 extends JFrame implements ActionListener{
	JTextField t;
	JPasswordField pa;
	JLabel l3;
	JButton b1,b2,b3;
	public Exercise4() throws Exception {
		setBounds(1000, 100, 300, 300);
		Font f = new Font("隶书", Font.BOLD+Font.ITALIC, 40);
//账号区		
		JPanel p = new JPanel(new GridLayout(4, 1));
		JPanel p1 = new JPanel();
		JLabel l1 = new JLabel("账号:");
		t = new JTextField(20);
		p1.add(l1);
		p1.add(t);
//密码区		
		JPanel p2 = new JPanel();
		JLabel l2 = new JLabel("密码:");
		pa = new JPasswordField(20);
		p2.add(l2);
		p2.add(pa);
//按钮区		
		JPanel p3 = new JPanel();
		b1 = new JButton("登陆");
		b2 = new JButton("退出");
		b1.addActionListener(this);
		b2.addActionListener(this);
		p3.add(b1);
		p3.add(b2);
//字体变色及其按钮区		
		JPanel p4 = new JPanel(new GridLayout(2, 1));
		l3 = new JLabel("变色字体",JLabel.CENTER);//标签居中要在标签定义时候改
		l3.setFont(f);
		b3 = new JButton("换色");
		b3.addActionListener(this);
		p4.add(l3);
		p4.add(b3);				
		p.add(p1);
		p.add(p2);
		p.add(p3);
		p.add(p4);
		add(p);
		setVisible(true);
		
	}
	
	public static void main(String[] args) throws Exception {		
		new Exercise4();
	}

	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1) {
			if(t.getText().equals("")||t.getText().equals(null)||pa.getText().equals("")||pa.getText().equals(null)) {
				JOptionPane.showMessageDialog(null, "输入不能为空");
			}
		}else if(e.getSource()==b2) {
			System.exit(0);
		}else if(e.getSource()==b3) {
			l3.setForeground(new Color(new Random().nextInt(255),new Random().nextInt(255),new Random().nextInt(255)));
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值