接口


import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

@SuppressWarnings("serial")
public class MyFrame1 extends JFrame {
	private JLabel lbl = new JLabel("Hello, world!");
	private JButton okButton = new JButton("OK");
	
	public MyFrame1() {
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());	// 使用流式布局管理器
		lbl.setFont(new Font("Consolas", Font.BOLD, 36));
		this.add(lbl);			// 向窗口添加标签控件
		// 为按钮绑定点击事件(添加一个监听器并重写回调方法)
		// 创建匿名内部类对象作为监听器绑定到按钮上
		okButton.addActionListener(new ActionListener() {	// 就地实例化ActionListener接口

			@Override
			public void actionPerformed(ActionEvent e) {	// 点击按钮时需要执行的回调方法
				// 在此处添加处理按钮点击事件的代码
				lbl.setVisible(!lbl.isVisible());
			}
			
		});
		this.add(okButton);		// 向窗口添加按钮控件
	}
	
	public static void main(String[] args) {
		new MyFrame1().setVisible(true);
	}
}



import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

@SuppressWarnings("serial")
public class MyFrame2 extends JFrame implements ActionListener {
	private JLabel lbl = new JLabel("Hello, world!");
	private JButton okButton = new JButton("OK");
	
	public MyFrame2() {
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());	// 使用流式布局管理器
		lbl.setFont(new Font("Consolas", Font.BOLD, 36));
		this.add(lbl);			// 向窗口添加标签控件
		
		// 用当前类的对象充当监听器
		// 由于当前类实现了ActionListener接口所以具备了充当监听器的能力
		okButton.addActionListener(this);
		this.add(okButton);		// 向窗口添加按钮控件
	}
	
	public static void main(String[] args) {
		new MyFrame2().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		lbl.setVisible(!lbl.isVisible());
	}
}


import java.awt.FlowLayout;
import java.awt.Font;
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;

@SuppressWarnings("serial")
public class MyFrame3 extends JFrame {
	
	// 内部类(内部类可以访问外部类的成员,包括private成员)
	public class MyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			lbl.setVisible(!lbl.isVisible());
		}

	}
	
	public class MyListener2 implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(null, "shit!!!");
		}
		
	}
	
	private JLabel lbl = new JLabel("Hello, world!");
	private JButton okButton = new JButton("OK");
	
	public MyFrame3() {
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());	// 使用流式布局管理器
		lbl.setFont(new Font("Consolas", Font.BOLD, 36));
		this.add(lbl);			// 向窗口添加标签控件
		
		// 创建内部类对象作为监听器
		okButton.addActionListener(new MyListener2());
		okButton.addActionListener(new MyListener());
		
		this.add(okButton);		// 向窗口添加按钮控件
	}
	
	public static void main(String[] args) {
		new MyFrame3().setVisible(true);
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值