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

第十一章 PowerPoint PPT Presentation


  • 111 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



  • 11.1AWT

  • 11.2

  • 11.3


11.1AWT

JDKJava(Abstract Window Toolkit, AWT)AWTjava.awt


Java.awt


1

componentComponentAWTComponentComponent


2

container


3

2windowpanel

WindowPanelContainer


4

Window2FrameDialog


(Frame)

  • frame

  • setSize()setVisible(true)


Frame


Frame


dialog

  • dialog

  • Dialog


--

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());

}

}


State:0

Title:FrmApp

Visible:true


5

Label


Label


Label


--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


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

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

lab.setFont(fnt);

fra.add(lab);

fra.setVisible(true);

}

}



6

java.awtButton


Button


java.awt.Button


--

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); }

}



7

java.awtTextField


TextField


TextField


--

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); }

}



11.2

Java


11.2

java.awt5

  • FlowLayout

  • BorderLayout

  • CardLayout

  • GridLayout

  • GridBagLayout

    java.lang.Object


11.2.1 BorderLayout

BorderLayout55EastWestSouthNorthCenter


BorderLayout


--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");


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);

}

}



11.2.2 FlowLayout

FlowLayout


FlowLayout


--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);


frm.add(but1);

frm.add(but2);

frm.add(txt1);

frm.add(txt2);

frm.setVisible(true);

}

}



11.2.3 GridLayout

GridLayout1


GridLayout


--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"};


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

{

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

}

frm.setVisible(true);

}

}



11.3

11.3.1

JDK1.1

delegation event model


1

event


2

Javajava.awt.eventActionEventWindowEvent


3


4


4

2


5.

  • Java


6


7


--

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;


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);

}

}


--

4static

import java.awt.*;

import java.awt.event.*;

class InnClassEvent

{

static Frame frm=new Frame();


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);

}


static class InnCla implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

frm.setBackground(Color.red);

}

}

}


11.3.2

1

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


Java.awt.event


2

Java



AWT


3

  • Java(adapt)Adapter


WindowAdapterWindowListenerWindowListener

windowClosing(WindowEvent e)

windowClosed(WindowEvent e)

windowOpened(WindowEvent e)

windowActivated(WindowEvent e)

windowDeactivated(WindowEvent e)

windowIconified(WindowEvent e)

windowDeiconified(WindowEvent e)



4


11.3.3 ActionEvent

ButtonListMenuItemTextFieldEnterActionEventActionEventactionPerformed()


11.3.3 ActionEvent

ActionEventEventObjectgetSource()ActionEventgetActionCommand()


--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;


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);

}


public void actionPerformed(ActionEvent e)

{

if(e.getSource()==btn1)

lbl.setText("button 1 clicked");

else

lbl.setText("button 2 clicked");

}

}


(a)(b)1(c) 2


11.3.4 ItemEvent

CheckboxListitem event

JavaItemEventItemEventItemListeneritemStsteChanged()

public void itemStateChanged(ItemEvent e)


--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;


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);

}


public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==chb1)

txt1.setText("green is selected");

else

if (e.getSource()==chb2)

txt1.setText("yellow is selected");

}

}


(a)(b)green(c) yellow


11.3.5 TextEvent

TextFieldTextAreaTextEventJavaTextEvent

TextEventTextListenertextValueChanged()

public void textValueChanged(TextEvent e)

TextFieldTextArea


--TextEvent

2

import java.awt.*;

import java.awt.event.*;

class TexEvent extends Frame implements TextListener

{

static TexEvent frm=new TexEvent();

static TextField txt1,txt2;


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);

}


public void textValueChanged(TextEvent e)

{

txt2.setText(txt1.getText());

}

}


(a)(b)1


11.3.6 KeyEvent

key event JavaKeyEvent


KeyEvent


KeyListener


--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;


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);

}


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"); }

}


:

(a)(b)r


--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;


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);

}


static class KeysAdapter extends KeyAdapter

{

public void keyTyped(KeyEvent e)

{

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

}

}

}


(a)(b)1t


11.3.7 MouseEvent

mouse eventJavaMouseEvent


MouseEvent


JavaMouseListenerMouseMotionListenerMouseEventMouseListener


MouseListener


MouseMotionListener2

  • 2MouseMotionListener


MouseMotionListener


--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;


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);

}


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");

}


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 ");

}

}


(a)(b)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;


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);

}


public void mouseMoved(MouseEvent e) //

{

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

}

public void mouseDragged(MouseEvent e) //

{

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

}

}


(a)(b)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;


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);

}


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

window eventJavaWindowEventWindowListener,


WindowListener


--WindowEvent

import java.awt.*;

import java.awt.event.*;

public class WinEvent extends Frame implements WindowListener

{

static WinEvent frm=new WinEvent();


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)

{ }


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");

}

}



11.4

JavaGraphicsGraphics


1Graphics

GraphicsXY0, 0x, yxy


Graphics


2

java.awt.Componentpaint()

public void paint(Graphics g)


2

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


--

11Painting

import javax.swing.*;

import java.awt.*;

public class Painting extends Frame

{

public Painting()

{ super("Painting");

setSize(200,150);

setVisible(true);

}


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);

}

}



  • Login