- 什么是事件?用户的哪些操作可能引发事件?简述Java语言的委托事件模型。
事件就是用户使用鼠标或键盘对窗口中的组件进行交互时所发生的事情。
如单击按钮、输入文字、单击鼠标等。
委托事件模型是指当事件所发生时,产生事件的对象即事件源,会把此“信息”转给事件监听者处理的一种方式,而这里所指的“信息”事实上就是java.awt.event事件类库里某个类所创建的对象,我们把它称为“事件对象”(event object)。事件对象表示事件的内容,对象内部封装了一个对事件源eventSource的引用和其他信息。 - 若要处理事件,就必须要有事件监听者,通常哪些对象可以担任监听者?
1)让包含“事件源”的对象来担任监听者;
2)定义内部类来担任监听者。 - Java语言把事件类分哪两种?这两种事件类的功能各是什么?
1)语义事件:直接继承自AWTEvent类
2)底层事件:继承自ComponentEvent类 - 写出组件所可能产生的事件的对应关系。
Button - ActionEvent
CheckBox - ActionEvent、ItemEvent
Component - ComponentEvent、FocusEvent、KeyEvent、MouseEvent
MenuItem - ActionEvent
Scroller - AdjustmentEvent
TextField - ActionEvent
TextArea - ActionEvent
Window - WindowEvent - 在进行事件处理时,可以使用多接口的方法,也可以尽可能地使用适配器类。使用适配器类的好处是什么?
当需要对某种事件进行处理时,只需让事件处理类继承事件所对应的适配器类,覆盖本次操作用到的事件处理方法即可,而不必实现无关的事件处理方法。 - 设计一个窗口,在窗口内摆放一个按钮,当不断地单击该按钮时,则在其上显示它被单击的次数。
import java.awt.*;
import java.awt.event.*;
public class exe13_6 {
static Frame frm = new Frame("习题13-6");
static Button btn = new Button("请点击");
static int count = 1;
public static void main(String[] args) {
frm.setLayout(null);
frm.setSize(300, 200);
btn.setBounds(100, 50, 100, 100);
frm.add(btn);
frm.setVisible(true);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setLabel("已点击" + count + "次");
count++;
}
});
}
}
- 设计一个窗口,在该窗口中添加一个List组件,该组件中有5门课程名称的选项。然后再在窗口中添加一个文本区,当选择List组件中的某个选项后,文本区中显示对该课程的介绍;当双击List组件中的某个选项后,文本区中显示该课程的开课时间。
import java.awt.*;
import java.awt.event.*;
public class exe13_7 extends Frame implements ItemListener, ActionListener {
static exe13_7 frm = new exe13_7();
static List list = new List();
static TextArea ta = new TextArea(5, 20);
public static void main(String[] args) {
frm.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));
frm.setSize(350, 200);
list.add("Course 1");
list.add("Course 2");
list.add("Course 3");
list.add("Course 4");
list.add("Course 5");
list.addItemListener(frm);
list.addActionListener(frm);
frm.add(list);
frm.add(ta);
frm.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
String course = list.getSelectedItem();
if (course == "Course 1") {
ta.setText("Course 1 introduction");
} else if (course == "Course 2") {
ta.setText("Course 2 introduction");
} else if (course == "Course 3") {
ta.setText("Course 3 introduction");
} else if (course == "Course 4") {
ta.setText("Course 4 introduction");
} else if (course == "Course 5") {
ta.setText("Course 5 introduction");
}
frm.setTitle("You choose [" + course + "]");
}
public void actionPerformed(ActionEvent e) {
String course = list.getSelectedItem();
if (course == "Course 1") {
ta.setText("Course 1 start time");
} else if (course == "Course 2") {
ta.setText("Course 2 start time");
} else if (course == "Course 3") {
ta.setText("Course 3 start time");
} else if (course == "Course 4") {
ta.setText("Course 4 start time");
} else if (course == "Course 5") {
ta.setText("Course 5 start time");
}
frm.setTitle("You double click [" + course + "]");
}
}
- 设计一个窗口,其中包含一个文本区、两个复选框和3个横向滚动条,其中滚动条用来调整红、绿、蓝三色的分量从0~255变化;两个复选框分别用于设定把滚动条调出的颜色应用于文本的前景还是背景。
import java.awt.*;
import java.awt.event.*;
public class exe13_8 extends Frame implements AdjustmentListener, ItemListener {
static exe13_8 frm = new exe13_8();
static Scrollbar sbR = new Scrollbar(Scrollbar.HORIZONTAL);
static Scrollbar sbG = new Scrollbar(Scrollbar.HORIZONTAL);
static Scrollbar sbB = new Scrollbar(Scrollbar.HORIZONTAL);
static Checkbox cb1 = new Checkbox("foreground");
static Checkbox cb2 = new Checkbox("background");
static TextArea ta = new TextArea("TextArea", 10, 20);
final static int FOREGROUND = 0;
final static int BACKGROUND = 1;
static int ground = 0;
public static void main(String[] args) {
frm.setTitle("exe13_8");
frm.setLocation(200, 150);
frm.setLayout(new GridLayout(6,1));
frm.setSize(400, 300);
sbR.setValues(1, 1, 0, 255);
sbG.setValues(1, 1, 0, 255);
sbB.setValues(1, 1, 0, 255);
sbR.addAdjustmentListener(frm);
sbG.addAdjustmentListener(frm);
sbB.addAdjustmentListener(frm);
CheckboxGroup grp = new CheckboxGroup();
cb1.setCheckboxGroup(grp);
cb2.setCheckboxGroup(grp);
cb1.addItemListener(frm);
cb2.addItemListener(frm);
frm.add(ta);
frm.add(cb1);
frm.add(cb2);
frm.add(sbR);
frm.add(sbG);
frm.add(sbB);
frm.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
Checkbox cb = (Checkbox) e.getSource();
if (cb == cb1) ground = 0;
else if (cb == cb2) ground = 1;
}
public void adjustmentValueChanged(AdjustmentEvent e) {
int colorR = sbR.getValue();
int colorG = sbG.getValue();
int colorB = sbB.getValue();
if (ground == FOREGROUND) ta.setForeground(new Color(colorR, colorG, colorB));
else if (ground == BACKGROUND) ta.setBackground(new Color(colorR, colorG, colorB));
}
}
- 编写一个应用程序,在其窗口内包含一个菜单条和一个文本区。菜单条包括“设置”和“操作”两个菜单。“操作”菜单包括“退出”菜单项,当用户选择“退出”菜单项时,则关闭窗口退出整个应用程序的运行;“设置”菜单包括“字体”和“风格”两个菜单项和一个“只读”复选按钮菜单项。“字体”菜单项包括TimesRoman、Courier和Helvetica3个子菜单项,“风格”菜单项包括“普通”、“黑体”、“斜体”3个子菜单项。当“只读”菜单项未被选中时,用户不能在文本区内输入字符。当用户选择其他菜单项时,文本区内的文字随之变化。
import java.awt.*;
import java.awt.event.*;
public class exe13_9 extends Frame implements ActionListener, ItemListener {
static exe13_9 frm = new exe13_9();
static MenuBar mb = new MenuBar();
static Menu m_settings = new Menu("设置");
static Menu m_operator = new Menu("操作");
static MenuItem mi_quit = new MenuItem("退出");
static Menu m_font = new Menu("字体");
static Menu m_style = new Menu("风格");
static CheckboxMenuItem cbmi = new CheckboxMenuItem("只读");
static MenuItem mi_font1 = new MenuItem("TimeRoman");
static MenuItem mi_font2 = new MenuItem("Courier");
static MenuItem mi_font3 = new MenuItem("Helvetica");
static MenuItem mi_style1 = new MenuItem("普通");
static MenuItem mi_style2 = new MenuItem("黑体");
static MenuItem mi_style3 = new MenuItem("斜体");
static TextArea ta = new TextArea("菜单程序设计", 10, 30);
public static void main(String[] args) {
frm.setTitle("exe13_9");
frm.setLocation(100, 80);
frm.setLayout(null);
frm.setSize(260, 170);
frm.add(ta);
ta.setBounds(40, 65, 180, 80);
mb.add(m_settings);
mb.add(m_operator);
m_settings.add(m_font);
m_settings.add(m_style);
m_settings.addSeparator();
m_settings.add(cbmi);
m_operator.add(mi_quit);
m_font.add(mi_font1);
m_font.add(mi_font2);
m_font.add(mi_font3);
m_style.add(mi_style1);
m_style.add(mi_style2);
m_style.add(mi_style3);
frm.setMenuBar(mb);
mi_quit.addActionListener(frm);
cbmi.addItemListener(frm);
mi_font1.addActionListener(frm);
mi_font2.addActionListener(frm);
mi_font3.addActionListener(frm);
mi_style1.addActionListener(frm);
mi_style2.addActionListener(frm);
mi_style3.addActionListener(frm);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
MenuItem mi = (MenuItem) e.getSource();
if (mi == mi_quit) {
frm.dispose();
System.exit(0);
}
Font font = ta.getFont();
String fontName = font.getName();
int style = font.getStyle();
if (mi == mi_font1) {
fontName = "Times New Roman";
} else if (mi == mi_font2) {
fontName = "Courier";
} else if (mi == mi_font3) {
fontName = "Helvetica";
}
if (mi == mi_style1) {
style = Font.PLAIN;
} else if (mi == mi_style2) {
style = Font.BOLD;
} else if (mi == mi_style3) {
style = Font.ITALIC;
}
ta.setFont(new Font(fontName, style, font.getSize()));
}
public void itemStateChanged(ItemEvent e) {
boolean readOnly = cbmi.getState();
if (readOnly) {
ta.setEditable(false);
} else {
ta.setEditable(true);
}
}
}
- 在第9题的基础上增加如下的功能:每当用户选中“只读”菜单项时,都将“字体”和“风格”两个菜单项变成灰色,使之不能被选中;而每当“只读”菜单项未被选中时,再将“字体”和“风格”两个菜单项恢复成可选状态。
import java.awt.*;
import java.awt.event.*;
public class exe13_10 extends Frame implements ActionListener, ItemListener {
static exe13_10 frm = new exe13_10();
static MenuBar mb = new MenuBar();
static Menu m_settings = new Menu("设置");
static Menu m_operator = new Menu("操作");
static MenuItem mi_quit = new MenuItem("退出");
static Menu m_font = new Menu("字体");
static Menu m_style = new Menu("风格");
static CheckboxMenuItem cbmi = new CheckboxMenuItem("只读");
static MenuItem mi_font1 = new MenuItem("TimeRoman");
static MenuItem mi_font2 = new MenuItem("Courier");
static MenuItem mi_font3 = new MenuItem("Helvetica");
static MenuItem mi_style1 = new MenuItem("普通");
static MenuItem mi_style2 = new MenuItem("黑体");
static MenuItem mi_style3 = new MenuItem("斜体");
static TextArea ta = new TextArea("菜单程序设计", 10, 30);
public static void main(String[] args) {
frm.setTitle("exe13_9");
frm.setLocation(100, 80);
frm.setLayout(null);
frm.setSize(260, 170);
frm.add(ta);
ta.setBounds(40, 65, 180, 80);
mb.add(m_settings);
mb.add(m_operator);
m_settings.add(m_font);
m_settings.add(m_style);
m_settings.addSeparator();
m_settings.add(cbmi);
m_operator.add(mi_quit);
m_font.add(mi_font1);
m_font.add(mi_font2);
m_font.add(mi_font3);
m_style.add(mi_style1);
m_style.add(mi_style2);
m_style.add(mi_style3);
frm.setMenuBar(mb);
mi_quit.addActionListener(frm);
cbmi.addItemListener(frm);
mi_font1.addActionListener(frm);
mi_font2.addActionListener(frm);
mi_font3.addActionListener(frm);
mi_style1.addActionListener(frm);
mi_style2.addActionListener(frm);
mi_style3.addActionListener(frm);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
MenuItem mi = (MenuItem) e.getSource();
if (mi == mi_quit) {
frm.dispose();
System.exit(0);
}
Font font = ta.getFont();
String fontName = font.getName();
int style = font.getStyle();
if (mi == mi_font1) {
fontName = "Times New Roman";
} else if (mi == mi_font2) {
fontName = "Courier";
} else if (mi == mi_font3) {
fontName = "Helvetica";
}
if (mi == mi_style1) {
style = Font.PLAIN;
} else if (mi == mi_style2) {
style = Font.BOLD;
} else if (mi == mi_style3) {
style = Font.ITALIC;
}
ta.setFont(new Font(fontName, style, font.getSize()));
}
public void itemStateChanged(ItemEvent e) {
boolean readOnly = cbmi.getState();
if (readOnly) {
ta.setEditable(false);
m_font.setEnabled(false);
m_style.setEnabled(false);
} else {
ta.setEditable(true);
m_font.setEnabled(true);
m_style.setEnabled(true);
}
}
}
注:练习均为博主自己编写,不是标准答案,可能存在问题,可以留言讨论。