1、外部类监听
package com.yfs.javase; import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//外部类监听
public class FrameDemo1 extends Frame {
Button but1 = new Button(" + ");
Button but2 = new Button(" - ");
TextField text = new TextField(20);
public FrameDemo1() {
super("event1外部类");
setLocation(200, 150);
// 设置布局
setLayout(new FlowLayout());
text.setText("100");
text.setEditable(false);
//创建监听器对象
MyListener listener = new MyListener(this);
//建立关联 监听器添加到按钮
but1.addActionListener(listener);
but2.addActionListener(listener);
//text.getText();//取text值
add(but1);
add(text);
add(but2);
setResizable(false);
pack();
// 默认隐藏 设置显示
setVisible(true);
} public static void main(String[] args) {
new FrameDemo1();
} } //开发监听器 实现监听器接口
class MyListener implements ActionListener {
private FrameDemo1 frame;
//通过构造方法强制传入窗体对象
public MyListener(FrameDemo1 frame) {
this.frame = frame;
}
//ActionEvent 事件对象
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("按钮被单击了....");
// //事件对象
System.out.println(e.getActionCommand());//控件字符
// System.out.println(e.getSource());//事件源
//从text取值
//依赖传入对象
//FrameDemo1 frame = new FrameDemo1();//不能创建窗体对象
int val = Integer.parseInt(frame.text.getText());
System.out.println("val = " + val);
String opt = e.getActionCommand().trim();
if("+".equals(opt)){
frame.text.setText(String.valueOf(val + 1));
} else if("-".equals(opt)){
frame.text.setText(String.valueOf(val - 1));
} } }
2.内部类监听
package com.yfs.javase; import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//内部类监听
public class FrameDemo2 extends Frame {
Button but1 = new Button(" + ");
Button but2 = new Button(" - ");
TextField text = new TextField(20);
public FrameDemo2() {
super("event1内部类");
setLocation(200, 150);
// 设置布局
setLayout(new FlowLayout());
text.setText("100");
text.setEditable(false);
//创建监听器对象
Listener listener = new Listener();
//建立关联 监听器添加到按钮
but1.addActionListener(listener);
but2.addActionListener(listener);
//text.getText();//取text值
add(but1);
add(text);
add(but2);
setResizable(false);
pack();
// 默认隐藏 设置显示
setVisible(true);
}
//内部类
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int val = Integer.parseInt(text.getText());
String opt = e.getActionCommand().trim();
System.out.println(opt);
if("+".equals(opt)){
text.setText(String.valueOf(val + 1));
} else if("-".equals(opt)){
text.setText(String.valueOf(val - 1));
}
} } public static void main(String[] args) {
new FrameDemo2();
} }
3.自监听
package com.yfs.javase; import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//自监听
public class FrameDemo3 extends Frame implements ActionListener {
Button but1 = new Button(" + ");
Button but2 = new Button(" - ");
TextField text = new TextField(20);
public FrameDemo3() {
super("event1自监听类");
setLocation(200, 150);
// 设置布局
setLayout(new FlowLayout());
text.setText("100");
text.setEditable(false); //建立关联 监听器添加到按钮
but1.addActionListener(this);
but2.addActionListener(this);
//text.getText();//取text值
add(but1);
add(text);
add(but2);
setResizable(false);
pack();
// 默认隐藏 设置显示
setVisible(true);
} public static void main(String[] args) {
new FrameDemo3();
} @Override
public void actionPerformed(ActionEvent e) {
//事件处理代码
int val = Integer.parseInt(text.getText());
String opt = e.getActionCommand().trim();
if("+".equals(opt)){
text.setText(String.valueOf(val + 1));
} else if("-".equals(opt)){
text.setText(String.valueOf(val - 1));
}
} }
4.匿名类监听
package com.yfs.javase; import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//匿名类监听
public class FrameDemo4 extends Frame {
Button but1 = new Button(" + ");
Button but2 = new Button(" - ");
TextField text = new TextField(20);
public FrameDemo4() {
super("event1匿名监听类");
setLocation(200, 150);
// 设置布局
setLayout(new FlowLayout());
text.setText("100");
text.setEditable(false); //建立关联 监听器添加到按钮
but1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int val = Integer.parseInt(text.getText()) + 1;
text.setText(String.valueOf(val)); }
});
but2.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent arg0) {
int val = Integer.parseInt(text.getText()) - 1;
text.setText(String.valueOf(val)); }
});
//text.getText();//取text值
add(but1);
add(text);
add(but2);
setResizable(false);
pack();
// 默认隐藏 设置显示
setVisible(true);
} public static void main(String[] args) {
new FrameDemo4();
} }
5.背景图片
package com.yfs.javase; import java.awt.FlowLayout;
import java.awt.Image; import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
//匿名类监听
public class FrameDemo5 extends JFrame { public FrameDemo5() {
super("event1匿名监听类");
setBounds(200, 150, 400, 400);
setLayout(new FlowLayout());
//标题图片
Image logo = new ImageIcon("img/m.gif").getImage();
setIconImage(logo);
//图片label
Icon ic = new ImageIcon("img\\index.gif");
JLabel jl = new JLabel(ic); JButton but = new JButton(new ImageIcon("img/1_03.gif"));
add(but);
add(jl);
setResizable(false);
//pack();
setVisible(true);
} public static void main(String[] args) {
new FrameDemo5();
} }