Java程序设计教案第五章.docx
《Java程序设计教案第五章.docx》由会员分享,可在线阅读,更多相关《Java程序设计教案第五章.docx(30页珍藏版)》请在冰点文库上搜索。
![Java程序设计教案第五章.docx](https://file1.bingdoc.com/fileroot1/2023-6/5/d864e7c1-e7c7-4e00-bdca-18de3edb38fe/d864e7c1-e7c7-4e00-bdca-18de3edb38fe1.gif)
Java程序设计教案第五章
教案纸
第5章Java的图形用户界面(6学时)
【主要讲授内容及时间分配】
5.1图形用户界面概述(25分钟)
5.2AWT中常用类的层次结构(20分钟)
5.3基本组件的使用(45分钟)
5.4布局管理器(45分钟)
5.5事件处理(90分钟)
5.6菜单、绘图类的使用(45分钟)
【重点与难点】
1、重点:
(1)基本组件的使用,包括Label、Button、TextField、TextArea、List、Checkbox和CheckboxGroup的构造方法和常用方法的使用。
(2)布局管理器的使用,包括FlowLayout、GridLayout、BorderLayout。
(3)事件处理。
2、难点:
事件处理。
【教学要求】
1、可以构造出图形用户界面;
2、可以为界面上的组件编写相应的事件处理代码;
3、可以构造带有菜单的应用程序;
4、可以写小游戏。
【实施方法】
课堂讲授,PPT配合
5.3基本组件的使用
1Label类
importjava.awt.*;
publicclassLabelDemoextendsFrame
{
publicLabelDemo()
{
Labellb1,lb2,lb3;
lb1=newLabel("LeftLabel");
lb2=newLabel("CenterLabel",Label.CENTER);
lb3=newLabel("RightLabel",Label.RIGHT);
lb3.setText("改变我的名字");
lb3.setAlignment(Label.CENTER);
//lb3.setVisible(false);
setLayout(newFlowLayout());
add(lb1);
add(lb2);
add(lb3);
}
publicstaticvoidmain(Stringargs[])
{
LabelDemoLd=newLabelDemo();
Ld.setVisible(true);
Ld.pack();
}
}
2Button类
importjava.awt.*;
publicclassMyButtons
{
publicstaticvoidmain(Stringargs[])
{
Framef=newFrame();
f.setLayout(newFlowLayout());
Buttonbutton1=newButton("Ok");
Buttonbutton2=newButton("Open");
Buttonbutton3=newButton("Close");
f.add(button1);
f.add(button2);
f.add(button3);
f.setSize(300,100);
f.setVisible(true);
}
}
3CheckBox类
importjava.awt.*;
publicclassCheckboxDemo2extendsFrame
{
publicCheckboxDemo2()
{
setLayout(newFlowLayout());
StringUniversity[]={"Tsinghua","Pecking","Fudan","Nanki","Tianjin"};
Checkboxc[]=newCheckbox[5];
Labellabel=newLabel("Thereare5University!
");
Labellabel2=newLabel("Thereare5University!
");
Labellabel3=newLabel("Thereare5University!
");
add(newLabel("PleasechoicetheUniversity:
"));
for(inti=0;i<5;i++)
{
c[i]=newCheckbox(University[i]);
add(c[i]);
}
add(label);
add(label2);
add(label3);
this.pack();
show();
}
publicstaticvoidmain(Stringargs[]){
CheckboxDemo2cbd=newCheckboxDemo2();
cbd.setSize(400,500);
}
}
4CheckBoxGroup类
importjava.awt.*;
importjava.awt.CheckboxGroup;
publicclassCheckboxGroupDemo2extendsFrame
{
publicCheckboxGroupDemo2()
{super("CheckboxGroupDemo2");
setLayout(newFlowLayout());
StringUniversity[]={"Tsinghua","Pecking","Fudan","Nanki","Tianjin"};
Checkboxc[]=newCheckbox[5];
Labellabel=newLabel("Thereare5University!
");
CheckboxGroupd=newCheckboxGroup();
add(newLabel("PleasechoicetheUniversity:
"));
for(inti=0;i<5;i++)
{
c[i]=newCheckbox(University[i],d,true);
add(c[i]);
}
//add(d);
add(label);
show();
}
publicstaticvoidmain(Stringargs[]){
CheckboxGroupDemo2cbg=newCheckboxGroupDemo2();
cbg.setSize(600,700);
}
}
5List类
importjava.awt.*;
publicclassListTestextendsFrame
{
publicListTest()
{
super("ListTest");
Listlt=newList(6,true);
setLayout(newFlowLayout());
lt.addItem("you");
lt.addItem("你");
lt.addItem("I");
lt.addItem("我");
lt.addItem("he");
lt.addItem("他");
add(lt);
}
publicstaticvoidmain(Stringargs[])
{
ListTestlt=newListTest();
lt.setVisible(true);
lt.setSize(500,400);
}
}
6TextField类
importjava.awt.*;
publicclassTextFieldTestextendsFrame
{
publicTextFieldTest()
{
super("TestTextField");
TextFieldtf=newTextField("&&&&&",20);
TextAreata=newTextArea("thisisanewtext",6,20);
setLayout(newFlowLayout());
tf.setEchoChar('*');
//tf.setText("9999999");
add(tf);
add(ta);
show();
}
publicstaticvoidmain(Stringargs[])
{
TextFieldTesttft=newTextFieldTest();
//tft.setVisible(false);
tft.setSize(500,400);
tft.pack();
}
}
5.4事件处理
事件类
事件源
事件监听接口
处理事件的方法
ActionEvent
Button
List(双击)
TextField
MenuItem
ActionListener
publicvoidactionPerformed(ActionEvente)
ItemEvent
Checkbox
List(单击)
Choice
CheckboxMenuItem
ItemListener
publicvoiditemStateChanged(ItemEvente)
WindowEvent
Frame
Dialog
WindowListener
publicvoidwindowOpened(WindowEvente)
publicvoidwindowClosing(WindowEvente)
publicvoidwindowClosed(WindowEvente)
publicvoidwindowIconified(WindowEvente)
publicvoidwindowDeiconified(WindowEvente)
publicvoidwindowActivated(WindowEvente)
publicvoidwindowDeactivated(WindowEvente)
MouseEvent
Frame
Dialog
Panel
Window
Canvas
MouseListener
publicvoidmouseClicked(MouseEvent e)
publicvoidmousePressed(MouseEvent e)
publicvoidmouseReleased(MouseEvent e)
publicvoidmouseEntered(MouseEvent e)
publicvoidmouseExited(MouseEvent e)
MouseEvent
Frame
Dialog
Panel
Window
Canvas
MouseMotionListener
publicvoidmouseDragged(MouseEvent e)
publicvoidmouseMoved(MouseEvent e)
1.事件源是Button
例1点击按钮关闭程序。
(法1)
importjava.awt.*;
importjava.awt.event.*;
publicclassMyFirstFrame1extendsFrameimplementsActionListener{
privateButtonquit=newButton("Quit");
publicMyFirstFrame1(){
super("TestWindow");
add(quit);
pack();
show();
quit.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
dispose();
//System.exit(0);
}
publicstaticvoidmain(Stringargs[]){
MyFirstFrame1mf=newMyFirstFrame1();
}
}
例1点击按钮关闭程序。
(法2)
importjava.awt.*;
importjava.awt.event.*;
publicclassMyFirstFrame2extendsFrame{
privateButtonquit=newButton("Quit");
publicMyFirstFrame2(){
super("TestWindow");
add(quit);
pack();
show();
quit.addActionListener(newButtonHander());
}
publicstaticvoidmain(Stringargs[])
{newMyFirstFrame2();
}
}
classButtonHandlerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
}
例2ClickMe(课本例5.4)
importjava.awt.*;
importjava.awt.event.*;
publicclassClickMeextendsFrameimplementsActionListener{
privateButtonquit=newButton("Quit");
privateButtonclick=newButton("Clickhere");
privateTextFieldtext=newTextField(10);
privatebooleansecondClick=false;
publicClickMe(){
super("ClickExample");
setLayout(newFlowLayout());
add(quit);
add(click);
click.addActionListener(this);
quit.addActionListener(this);
add(text);
pack();
show();
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==quit)
System.exit(0);
elseif(e.getSource()==click){
if(secondClick)
text.setText("notagain!
");
else
text.setText("Uh,ittickless");
secondClick=!
secondClick;
}
}
publicstaticvoidmain(Stringargs[]){
ClickMemyFrame=newClickMe();
}
}
2.事件源是List
课本例5.13
importjava.awt.*;
importjava.awt.event.*;
publicclassTaskListextendsFrameimplementsActionListener{
privateButtonadd=newButton("添加");
privateButtondel=newButton("删除");
privateButtonup=newButton("增加优先级");
privateButtondown=newButton("降低优先级");
privateListlist=newList();
privateTextFieldtaskInput=newTextField();
privateLabelpriorityLabel=newLabel("改变优先级");
privateLabeltaskLabel=newLabel("工作事项:
");
privateclassWindowCloserextendsWindowAdapter{
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
publicTaskList(){
super("工作事项表");
setup();
add.addActionListener(this);
del.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
addWindowListener(newWindowCloser());
list.addActionListener(this);
}
publicstaticvoidmain(Stringargs[]){
TaskListtl=newTaskList();
tl.pack();
tl.show();
}
privatevoidsetup(){
Panelbuttons=newPanel();
buttons.setLayout(newFlowLayout());
buttons.add(add);
buttons.add(del);
Panelpriorities=newPanel();
priorities.setLayout(newFlowLayout());
priorities.add(up);
priorities.add(priorityLabel);
priorities.add(down);
Panelinput=newPanel();
input.setLayout(newBorderLayout());
input.add("West",taskLabel);
input.add("Center",taskInput);
Paneltop=newPanel();
top.setLayout(newGridLayout(2,1));
top.add(input);
top.add(priorities);
setLayout(newBorderLayout());
add("Center",list);
add("South",buttons);
add("North",top);
}
publicvoidactionPerformed(ActionEvente){
if((e.getSource()==add)&&(!
taskInput.getText().equals("")))
handleAdd(taskInput.getText().trim());
elseif((e.getSource()==del)&&(list.getSelectedIndex()>=0))
handleDel(list.getSelectedIndex());
elseif((e.getSource()==up)&&(list.getSelectedIndex()>0))
handleIncPriority(list.getSelectedIndex());
elseif((e.getSource()==down)&&(list.getSelectedIndex()>=0))
handleDecPriority(list.getSelectedIndex());
elseif(e.getSource()==list)
taskInput.setText(list.getSelectedItem());
taskInput.requestFocus();
}
privatevoidhandleAdd(StringnewTask){
list.add(newTask);
list.select(list.getItemCount()-1);
taskInput.setText("");
}
privatevoidhandleDel(intpos){
list.remove(pos);
list.select(pos);
}
privatevoidhandleIncPriority(intpos){
Stringitem=list.getItem(pos);
list.remove(pos);
list.add(item,pos-1);
list.select(pos-1);
}
privatevoidhandleDecPriority(intpos){
if(posStringitem=list.getItem(pos);
list.remove(pos);
list.add(item,pos+1);
list.select(pos+1);
}
}
}
3.事件源是Window(WindowListener和WindowAdapter)
importjava.awt.*;
importjava.awt.event.*;
publicclassMultipleEventTesterextendsFrameimplementsWindowListener,MouseListener,KeyListener{
publicMultipleEventTester(){
addKeyListener(this);
addWindowListener(this);
addMouseListener(this);
setSize(400,400);
show();
}
//窗口事件处理方法
publicvoidwindowClosing(WindowEventwe){
System.exit(0);
}
publicvoidwindowOpened(WindowEventwe){
System.out.pr