This presentation is the property of its rightful owner.
Sponsored Links
1 / 129

第十一章 PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on
  • Presentation posted in: General

第十一章. 图形用户界面设计. 图形用户界面设计. 11.1 AWT 组件概述 11.2 布局管理 11.3 事件处理. 11.1 AWT 组件概述. 早期的 JDK 版本中提供了 Java 抽象窗口工具集 (Abstract Window Toolkit, AWT) ,其目的是为程序员创建图形用户界面提供支持。 AWT 组件定义在 java.awt 包中,包括组件类、组件布局类等 。. Java.awt 的继承关系. 1 .组件.

Download Presentation

第十一章

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


3553409


3553409

  • 11.1AWT

  • 11.2

  • 11.3


11 1 awt

11.1AWT

JDKJava(Abstract Window Toolkit, AWT)AWTjava.awt


Java awt

Java.awt


3553409

1

componentComponentAWTComponentComponent


3553409

2

container


3553409

3

2windowpanel

WindowPanelContainer


3553409

4

Window2FrameDialog


Frame

(Frame)

  • frame

  • setSize()setVisible(true)


Frame1

Frame


Frame2

Frame


Dialog

dialog

  • dialog

  • Dialog


3553409

--

import java.awt.*;

class FrmApp

{

static Frame fra=new Frame("FrmApp");

public static void main(String args[])

{

fra.setSize(250,150);

fra.setLocation(100,200);

fra.setVisible(true);

System.out.println("State:"+fra.getState());

System.out.println("Title:"+fra.getTitle());

System.out.println("Visible:"+fra.isVisible());

}

}


3553409

State:0

Title:FrmApp

Visible:true


3553409

5

Label


Label

Label


Label1

Label


3553409

--1

import java.awt.*;

class LabApp

{

public static void main(String args[])

{

Frame fra=new Frame("LabApp");

Label lab=new Label(); //1

fra.setSize(250,150);

lab.setText("This is a label"); //

lab.setAlignment(Label.CENTER);

lab.setBackground(Color.white); //Component


3553409

lab.setForeground(Color.black); //Component

Font fnt=new Font("Serief", Font.ITALIC+Font.BOLD,22);

lab.setFont(fnt);

fra.add(lab);

fra.setVisible(true);

}

}


3553409


3553409

6

java.awtButton


Button

Button


Java awt button

java.awt.Button


3553409

--

import java.awt.*;

class Buttapp

{

public static void main(String args[])

{

Frame fra=new Frame("ButtApp");

fra.setSize(250,170);

fra.setLayout(null); //

Button butt=new Button("click");

butt.setSize(100,50);

butt.setLocation(75,60);

fra.add(butt);

fra.setVisible(true); }

}


3553409


3553409

7

java.awtTextField


Textfield

TextField


Textfield1

TextField


3553409

--

import java.awt.*;

class TextApp

{

public static void main(String args[])

{

Frame fra=new Frame("");

TextField txt1=new TextField(50);

TextField txt2=new TextField("Text Field", 50);

fra.setBounds(0,0,300,200);

fra.setLayout(null);

txt1.setBounds(50,50,130,20); //

txt2.setBounds(50,100,130,30);

fra.add(txt1); fra.add(txt2);

fra.setVisible(true); }

}


3553409


3553409

11.2

Java


3553409

11.2

java.awt5

  • FlowLayout

  • BorderLayout

  • CardLayout

  • GridLayout

  • GridBagLayout

    java.lang.Object


11 2 1 borderlayout

11.2.1 BorderLayout

BorderLayout55EastWestSouthNorthCenter


Borderlayout

BorderLayout


Borderlayout1

--BorderLayout

5BorderLayout

import java.awt.*;

class BorLay

{

public static void main(String args[])

{

Frame frm=new Frame("BorderLayout");

BorderLayout layout=new BorderLayout(5,7);

frm.setBounds(0,0,300,200);

frm.setLayout(layout);

Button butN,butS,butW,butE,butC;

butN=new Button("north button");

butS=new Button("south button");


3553409

butW=new Button("west button");

butE=new Button("east button");

butC=new Button("center button");

frm.add(butN, BorderLayout.NORTH);

frm.add(butS, BorderLayout.SOUTH);

frm.add(butW, BorderLayout.WEST);

frm.add(butE, BorderLayout.EAST);

frm.add(butC, BorderLayout.CENTER);

frm.setVisible(true);

}

}


3553409


11 2 2 flowlayout

11.2.2 FlowLayout

FlowLayout


Flowlayout

FlowLayout


Flowlayout1

--FlowLayout

import java.awt.*;

import javax.swing.*;

public class FlowLay

{

public static void main(String args[])

{

Frame frm=new Frame("BorderLayout");

FlowLayout layout=new FlowLayout();

frm.setBounds(0,0,200,200);

frm.setLayout(layout);

Button but1,but2;

TextField txt1,txt2;

but1=new Button("button 1");

but2=new Button("button 2");

txt1=new TextField("text 1",10);

txt2=new TextField("text 2",10);


3553409

frm.add(but1);

frm.add(but2);

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}

}


3553409


11 2 3 gridlayout

11.2.3 GridLayout

GridLayout1


Gridlayout

GridLayout


Gridlayout1

--GridLayout

import java.awt.*;

public class GridLay

{

public static void main(String args[])

{

Frame frm=new Frame("GridLayout");

GridLayout layout=new GridLayout(2,2); //22GridLayout

frm.setBounds(0,0,200,200);

frm.setLayout(layout);

String names[]={ "butt1","butt2","butt3","butt4"};


3553409

for(int i=0; i<names.length; i++)

{

frm.add( new Button(names[i]));

}

frm.setVisible(true);

}

}


3553409


3553409

11.3

11.3.1

JDK1.1

delegation event model


3553409

1

event


3553409

2

Javajava.awt.eventActionEventWindowEvent


3553409

3


3553409

4


3553409

4

2


3553409

5.

  • Java


3553409

6


3553409

7


3553409

--

import java.awt.*;

import java.awt.event.*;

class ButtEventApp extends Frame implements ActionListener

{

static ButtEventApp frm=new ButtEventApp();

public static void main(String args[])

{

frm.setTitle("ButtEventApp");

frm.setSize(300,160);

frm.setLayout(null);

Button btn;


3553409

btn=new Button("push");

btn.setBounds(120,80,60,30);

btn.addActionListener(frm);

frm.add(btn,BorderLayout.CENTER);

frm.setVisible(true);

}

public void actionPerformed (ActionEvent e)

{

frm.setBackground(Color.red);

}

}


3553409

--

4static

import java.awt.*;

import java.awt.event.*;

class InnClassEvent

{

static Frame frm=new Frame();


3553409

public static void main(String args[])

{

frm.setTitle("ButtEventApp");

frm.setSize(300,160);

frm.setLayout(null);

Button btn;

btn=new Button("push");

btn.setBounds(120,80,60,30);

btn.addActionListener(new InnCla());

frm.add(btn,BorderLayout.CENTER);

frm.setVisible(true);

}


3553409

static class InnCla implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

frm.setBackground(Color.red);

}

}

}


11 3 2

11.3.2

1

Javajava.awt.eventAWTEventjava.util.EventObjectEventObjectjava.lang.Object


Java awt event

Java.awt.event


3553409

2

Java


3553409


3553409

AWT


3553409

3

  • Java(adapt)Adapter


3553409

WindowAdapterWindowListenerWindowListener

windowClosing(WindowEvent e)

windowClosed(WindowEvent e)

windowOpened(WindowEvent e)

windowActivated(WindowEvent e)

windowDeactivated(WindowEvent e)

windowIconified(WindowEvent e)

windowDeiconified(WindowEvent e)


3553409


3553409

4


11 3 3 actionevent

11.3.3 ActionEvent

ButtonListMenuItemTextFieldEnterActionEventActionEventactionPerformed()


11 3 3 actionevent1

11.3.3 ActionEvent

ActionEventEventObjectgetSource()ActionEventgetActionCommand()


Actionevent

--ActionEvent

button1button221

import java.awt.*;

import java.awt.event.*;

class ActEvent extends Frame implements ActionListener

{

static ActEvent frm=new ActEvent();

static Button btn1,btn2;

static Label lbl;


3553409

public static void main(String args[])

{

frm.setTitle("ActionEvent");

frm.setSize(240,160);

frm.setLayout(new FlowLayout());

btn1=new Button(" button 1 ");

btn2=new Button(" button 2 ");

lbl=new Label(" no clicking ");

btn1.addActionListener(frm);

btn2.addActionListener(frm);

frm.add(btn1);

frm.add(btn2);

frm.add(lbl);

frm.setVisible(true);

}


3553409

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==btn1)

lbl.setText("button 1 clicked");

else

lbl.setText("button 2 clicked");

}

}


3553409

(a)(b)1(c) 2


11 3 4 itemevent

11.3.4 ItemEvent

CheckboxListitem event

JavaItemEventItemEventItemListeneritemStsteChanged()

public void itemStateChanged(ItemEvent e)


Itemevent

--ItemEvent

greenyellow21

import java.awt.*;

import java.awt.event.*;

class IteEvent extends Frame implements ItemListener

{

static IteEvent frm=new IteEvent();

static Checkbox chb1,chb2;

static TextField txt1;


3553409

public static void main(String args[])

{

frm.setTitle("ItemEvent");

frm.setSize(240,160);

frm.setLayout(new FlowLayout());

CheckboxGroup grp=new CheckboxGroup();

chb1=new Checkbox("green");

chb2=new Checkbox("yellow");

txt1=new TextField(" None is selected ");

chb1.setCheckboxGroup(grp);

chb2.setCheckboxGroup(grp);

chb1.addItemListener(frm);

chb2.addItemListener(frm);

frm.add(chb1); frm.add(chb2);

frm.add(txt1); frm.setVisible(true);

}


3553409

public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==chb1)

txt1.setText("green is selected");

else

if (e.getSource()==chb2)

txt1.setText("yellow is selected");

}

}


3553409

(a)(b)green(c) yellow


11 3 5 textevent

11.3.5 TextEvent

TextFieldTextAreaTextEventJavaTextEvent

TextEventTextListenertextValueChanged()

public void textValueChanged(TextEvent e)

TextFieldTextArea


Textevent

--TextEvent

2

import java.awt.*;

import java.awt.event.*;

class TexEvent extends Frame implements TextListener

{

static TexEvent frm=new TexEvent();

static TextField txt1,txt2;


3553409

public static void main(String args[])

{ frm.setTitle("TextEvent");

frm.setSize(240,160);

frm.setLayout(new FlowLayout());

txt1=new TextField(20);

txt2=new TextField(20);

txt1.addTextListener(frm);

frm.add(txt1);

txt2.setEditable(false);

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}


3553409

public void textValueChanged(TextEvent e)

{

txt2.setText(txt1.getText());

}

}


3553409

(a)(b)1


11 3 6 keyevent

11.3.6 KeyEvent

key event JavaKeyEvent


Keyevent

KeyEvent


Keylistener

KeyListener


Textevent1

--TextEvent

11

import java.awt.*;

import java.awt.event.*;

public class KeysEvent extends Frame implements KeyListener

{ static KeysEvent frm=new KeysEvent();

static TextField txf;

static TextArea txa;


3553409

public static void main(String args[])

{

frm.setTitle("KeyEvent"); frm.setSize(240,200);

frm.setLayout(new FlowLayout());

txf=new TextField(20); txa=new TextArea(6,20);

txa.setEditable(false);

txf.addKeyListener(frm);

frm.add(txf); frm.add(txa);

frm.setVisible(true);

}


3553409

public void keyPressed(KeyEvent e)

{ txa.setText("");

txa.append(e.getKeyChar()+" is pressed!\n");

}

public void keyReleased(KeyEvent e)

{ txa.append(e.getKeyChar()+" is released!\n"); }

public void keyTyped(KeyEvent e)

{ txa.append(e.getKeyChar()+" is typed!\n"); }

}


3553409

:

(a)(b)r


Keyadapter textevent

--KeyAdapterTextEvent

21KeyAdapter1x is enteredx

import java.awt.*;

import java.awt.event.*;

public class KeysEvent2 extends Frame

{

static KeysEvent2 frm=new KeysEvent2();

static TextField txt1,txt2;


3553409

public static void main(String args[])

{

frm.setTitle("KeyEvent");

frm.setSize(240,130);

frm.setLayout(new FlowLayout());

txt1=new TextField(20); txt2=new TextField(20);

txt2.setEditable(false);

txt1.addKeyListener(new KeysAdapter());

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}


3553409

static class KeysAdapter extends KeyAdapter

{

public void keyTyped(KeyEvent e)

{

txt2.setText(e.getKeyChar()+" is entered!" );

}

}

}


3553409

(a)(b)1t


11 3 7 mouseevent

11.3.7 MouseEvent

mouse eventJavaMouseEvent


Mouseevent

MouseEvent


3553409

JavaMouseListenerMouseMotionListenerMouseEventMouseListener


Mouselistener

MouseListener


3553409

MouseMotionListener2

  • 2MouseMotionListener


Mousemotionlistener

MouseMotionListener


Mouselistener mouseevent

--MouseListenerMouseEvent

2121

import java.awt.*;

import java.awt.event.*;

public class MouEvent extends Frame implements MouseListener

{

static MouEvent frm=new MouEvent();

static TextArea txa1,txa2;


3553409

public static void main(String args[])

{

frm.setTitle("MouEvent"); frm.setSize(240,300);

frm.setLayout(new FlowLayout());

txa1=new TextArea(5,30);

txa2=new TextArea(8,30);

txa2.setEditable(false);

txa1.addMouseListener(frm);

frm.add(txa1); frm.add(txa2);

frm.setVisible(true);

}


3553409

public void mouseEntered(MouseEvent e) //

{

txa2.setText("Mouse enters txa1\n");

}

public void mouseClicked(MouseEvent e) //

{

txa2.append("Mouse is

clisked at["+e.getX()+","+e.getY()+"]\n");

}

public void mousePressed(MouseEvent e) //

{

txa2.append("Mouse is pressed at ["+e.getX()+","+e.getY()+"]\n");

}


3553409

public void mouseReleased(MouseEvent e) //

{

txa2.append("Mouse is released at ["+e.getX()+","+e.getY()+"]\n");

}

public void mouseExited(MouseEvent e) //

{

txa2.append("Mouse exits from txa1 ");

}

}


3553409

(a)(b)MouseEvent


Mousemotion listener mouseevent

--MouseMotionListenerMouseEvent

1211

import java.awt.*;

import java.awt.event.*;

public class MouEvent2 extends Frame implements MouseMotionListener

{

static MouEvent2 frm=new MouEvent2();

static TextArea txa;

static TextField txt1,txt2;


3553409

public static void main(String args[])

{

frm.setTitle("MouEvent");

frm.setSize(240,200);

frm.setLayout(new FlowLayout());

txa=new TextArea(5,30);

txt1=new TextField(30);

txt2=new TextField(30);

txa.setEditable(false);

txa.addMouseMotionListener(frm);

frm.add(txa);

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}


3553409

public void mouseMoved(MouseEvent e) //

{

txt1.setText("Mouse is moved in txa");

}

public void mouseDragged(MouseEvent e) //

{

txt2.setText("Mouse is dragged in txa");

}

}


3553409

(a)(b)MouseEvent


Mousemotion adapter mouseevent

--MouseMotionAdapterMouseEvent

import java.awt.*;

import java.awt.event.*;

public class MouEvent3 extends Frame

{

static MouEvent2 frm=new MouEvent2();

static TextArea txa;

static TextField txt1,txt2;


3553409

public static void main(String args[])

{ frm.setTitle("MouEvent");

frm.setSize(240,200);

frm.setLayout(new FlowLayout());

txa=new TextArea(5,30);

txt1=new TextField(30);

txt2=new TextField(30);

txa.setEditable(false);

txa.addMouseMotionListener(new MouMotionAdapter());

frm.add(txa);

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}


3553409

static class MouMotionAdapter extends MouseMotionAdapter

{

public void mouseMoved(MouseEvent e) //

{ txt1.setText("Mouse is moved in txa"); }

public void mouseDragged(MouseEvent e) //

{ txt2.setText("Mouse is dragged in txa"); }

}

}


11 3 8 windowevent

11.3.8 WindowEvent

window eventJavaWindowEventWindowListener,


Windowlistener

WindowListener


Windowevent

--WindowEvent

import java.awt.*;

import java.awt.event.*;

public class WinEvent extends Frame implements WindowListener

{

static WinEvent frm=new WinEvent();


3553409

public static void main(String args[])

{ frm.setTitle("MouEvent");

frm.setSize(240,200);

frm.addWindowListener(frm);

frm.setVisible(true);

}

public void windowClosing(WindowEvent e)

{ System.out.println("windowClosing() method");

System.exit(0); }

public void windowClosed(WindowEvent e)

{ }


3553409

public void windowActivated(WindowEvent e) { }

public void windowDeactivated(WindowEvent e) { }

public void windowIconified(WindowEvent e)

{ System.out.println("windowIconified() method");

}

public void windowDeiconified(WindowEvent e)

{ System.out.println("windowDeiconified() method");

}

public void windowOpened(WindowEvent e)

{ System.out.println("windowOpened() method");

}

}


3553409


3553409

11.4

JavaGraphicsGraphics


1 graphics

1Graphics

GraphicsXY0, 0x, yxy


Graphics

Graphics


3553409

2

java.awt.Componentpaint()

public void paint(Graphics g)


3553409

2

paint()Graphicsgpaint()paint()Javapaint()paint()


3553409

--

11Painting

import javax.swing.*;

import java.awt.*;

public class Painting extends Frame

{

public Painting()

{ super("Painting");

setSize(200,150);

setVisible(true);

}


3553409

public static void main(String args[])

{ Painting app=new Painting();

}

public void paint(Graphics g)

{ g.setColor(Color.red);

g.drawLine(50,50,150,50);

g.drawRect(50,70,100,50);

Font fnt=new Font("dialog", Font.ITALIC+Font.BOLD,15);

g.setFont(fnt);

g.drawString("Painting",70,100);

}

}


3553409


  • Login