JAVA Programming Language Chapter 6 Windows Programming Juho

  • Slides: 67
Download presentation
JAVA Programming Language Chapter 6. Windows Programming Juho Kim Department of Computer Science and

JAVA Programming Language Chapter 6. Windows Programming Juho Kim Department of Computer Science and Engineering Sogang University 6 -1

Windows programming Event-Oriented Programming – Delegation Event Model Keyboard Event Mouse Event AWT Components

Windows programming Event-Oriented Programming – Delegation Event Model Keyboard Event Mouse Event AWT Components - Button – Checkbox, Checkbox Group: Radio Buttons, Choice, Canvas, Text. Field, Text. Area, List, Dialog, Scrollbar, Menu, Checkbox. Menu. Item, Popup Menu, Printing 6 -2

Event-Oriented Programming – Events : Objects that describes what happened • When the user

Event-Oriented Programming – Events : Objects that describes what happened • When the user performs an action at the user interface level(clicks a mouse or presses a key), this causes an event to be issued. A number of different types of event classes exist to describe different categories of user action. – Event sources : The generator of an event • For example, a mouse click on a Button components generates an Action. Event with the button as the source. The Action. Event instance is an object that contains information about the events that just took place. It contains: – get. Action. Command() : returns the command name associated with action – get. Modifiers() : returns any modifiers held during the action – Event handlers : A method that receives an event object, deciphers it, and processes the user’s interaction 6 -3

Delegation event model Panel and Frame event handlers Frame Panel Action event Button Action

Delegation event model Panel and Frame event handlers Frame Panel Action event Button Action handler action. Performed (Action. Event e) { …. . } – Events are sent to the component from which the event originated, but it is up to each component to propagate the event to one or more registered classes called listeners. Listeners contain event handlers that can receive and process the event. In this way, the event handler can be in an object separate from the component. Listeners are classes that implement the Event. Listener interface. 6 -4

Test. Button. java import java. awt. *; public class Test. Button { public static

Test. Button. java import java. awt. *; public class Test. Button { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me"); b. add. Action. Listener(new Button. Handler()); f. add(b, Border. Layout. CENTER); f. pack(); f. set. Visible(true); } } import java. awt. event. *; public class Button. Handler implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Action occurred"); System. out. println("Button's label is : ” + e. get. Action. Command()); } } Test. Button. java Button. Handler. java 6 -5

– The Button class has an add. Action. Listerner method - Action. Listener –

– The Button class has an add. Action. Listerner method - Action. Listener – The Action. Listener interface defines a single method, action. Performed, which receives an Action. Event – When a Button object is created, it can have an object registered as a listener for Action. Events through the add. Action. Listener() method. The registered listener is instantiated from a class that implements the Action. Listener interface. – When the Button object is clicked on with the mouse, and Action. Event is sent. The Action. Event is received through the action. Performed() method of any Action. Listener that is registered on the button through its add. Action. Listener() method. – The method get. Action. Command() of the Action. Event class returns the command name associated with this action. 6 -6

Event Hierarchy Action. Event Container. Event Adjustment Event java. awt. event Focus. Event Component.

Event Hierarchy Action. Event Container. Event Adjustment Event java. awt. event Focus. Event Component. Event Key. Event Input. Event Item. Event Mouse. Event Window. Event Text. Event 6 -7

Method Categories and Interfaces Category Action Item Mouse motion Interface Name Action. Listener Item.

Method Categories and Interfaces Category Action Item Mouse motion Interface Name Action. Listener Item. Listener Mouse. Motion. Listener Mouse button Mouse. Listener Key. Listener Focus. Listener Adjustment. Listener 6 -8 Methods action. Performed(Action. Event) item. State. Changed(Item. Event) mouse. Dragged(Mouse. Event) mouse. Moved(Mouse. Event) mouse. Pressed(Mouse. Event) mouse. Released(Mouse. Event) mouse. Entered(Mouse. Event) mouse. Exited(Mouse. Event) mouse. Clicked(Mouse. Event) key. Pressed(Key. Event) key. Released(Key. Event) key. Typed(Key. Event) focus. Gained(Focus. Event) focus. Lost(Focus. Event) adjustment. Value. Changed (Adjustment. Event)

Method Categories and Interfaces Category Interface Name Methods Window. Listener Component. Listener Container. Listener

Method Categories and Interfaces Category Interface Name Methods Window. Listener Component. Listener Container. Listener Text. Listener window. Closing(Window. Event) window. Opened(Window. Event) window. Iconified(Window. Event) window. Deiconified(Window. Event) window. Closed(Window. Event) window. Activated(Window. Event) window. Deactivated(Window. Event) component. Moved(Component. Event) component. Hidden(Component. Event) component. Resized(Component. Event) component. Shown(Component. Event) component. Added(Container. Event) component. Removed(Container. Event) text. Value. Changed(Text. Event) 6 -9

Keyboard event methods 6 -10

Keyboard event methods 6 -10

ev. Kbd. java import java. awt. *; import java. awt. event. *; public class

ev. Kbd. java import java. awt. *; import java. awt. event. *; public class ev. Kbd implements Key. Listener { private Frame f; public ev. Kbd() { f = new Frame("Key. Event Test"); f. set. Background(Color. white); f. add. Key. Listener(this); f. set. Size(200, 200); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } } 6 -11

ev. Kbd. java public void key. Typed(Key. Event e) { Graphics g = f.

ev. Kbd. java public void key. Typed(Key. Event e) { Graphics g = f. get. Graphics(); g. set. Font(new Font("바탕체", Font. PLAIN, 20)); g. clear. Rect(0, 0, 200); // delete privious printed value g. draw. String("키 : " + e. get. Key. Char(), 30, 100); g. draw. String("키코드 : " + (int) e. get. Key. Char(), 30, 130); } public void key. Pressed(Key. Event e) { } public void key. Released(Key. Event e) { } //----------------------------------public static void main(String[] args) { ev. Kbd mf = new ev. Kbd(); // frame construction } } 6 -12

Special Keys "Key typed" events – are higher-level and generally do not depend on

Special Keys "Key typed" events – are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e. g. , 'a'). "Key pressed" and "key released" events – are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e. g. , action keys, modifier keys, etc. ). The key being pressed or released is indicated by the get. Key. Code method, which returns a virtual key code. Virtual key codes – are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (like "A", which comes from shift and "a"). For example, pressing the Shift key will cause a KEY_PRESSED event with a VK_SHIFT key. Code, while pressing the 'a' key will result in a VK_A key. Code. After the 'a' key is released, a KEY_RELEASED event will be fired with VK_A. Separately, a KEY_TYPED event with a key. Char value of 'A' is generated. 6 -13

ev. Tuk. Su. Key. java import java. awt. *; import java. awt. event. *;

ev. Tuk. Su. Key. java import java. awt. *; import java. awt. event. *; public class ev. Tuk. Su. Key implements Key. Listener { private Frame f; private int x=250, y=150; public ev. Tuk. Su. Key () { f = new Frame("Key. Event Test"); f. set. Background(Color. white); f. add. Key. Listener(this); f. set. Size(200, 200); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } public void key. Pressed(Key. Event e) { int l=10; char c = e. get. Key. Char(); int key. Code = e. get. Key. Code(); int modifiers = e. get. Modifiers(); Graphics g = f. get. Graphics(); if (key. Code == 16) // VK_SHIFT l +=10; if (key. Code == 17) // VK_CONTROL l +=20; switch (key. Code) { case 38 : y -= l; break; // VK_UP case 40 : y += l; break; // VK_DOWN case 37 : x -= l; break; // VK_LEFT case 39 : x += l; break; // VK_RIGHT case 35 : System. exit(0); } g. fill. Oval(x, y, l, l); // draw oval } public void key. Typed(Key. Event e) { } public void key. Released(Key. Event e) { } public static void main(String[] args) { ev. Tuk. Su. Key mf = new ev. Tuk. Su. Key (); }} 6 -14

Mouse event public abstract class Mouse. Adapter extends Object implements Mouse. Listener – An

Mouse event public abstract class Mouse. Adapter extends Object implements Mouse. Listener – An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects. – Mouse events let you track when a mouse is pressed, released, clicked, when it enters a component, and when it exits. (To track mouse moves and mouse drags, use the Mouse. Motion. Adapter. ) – Extend this class to create a Mouse. Event listener and override the methods for the events of interest. (If you implement the Mouse. Listener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about. ) – Create a listener object using the extended class and then register it with a component using the component's add. Mouse. Listener method. When a mouse button is pressed, released, or clicked (pressed and released), or when the mouse cursor enters or exits the component, the relevant method in the listener object is invoked and the Mouse. Event is passed to it. 6 -15

Mouse. Listener 6 -16

Mouse. Listener 6 -16

Mouse. Motion. Listener public abstract class Mouse. Motion. Adapter extends Object implements Mouse. Motion.

Mouse. Motion. Listener public abstract class Mouse. Motion. Adapter extends Object implements Mouse. Motion. Listener – Mouse motion events occur when a mouse is moved or dragged. (Many such events will be generated in a normal program. To track clicks and other mouse events, use the Mouse. Adapter. ) 6 -17

Multiple listeners – The AWT event listening framework allows multiple listeners to be attached

Multiple listeners – The AWT event listening framework allows multiple listeners to be attached to the same component. – The listener mechanism allows you to call an add. XXXListener() method as many times as needed, and you can specify as many different listeners as your design requires. All registered listeners have their handler methods called when the event occurs. – In the next example, f. add. Mouse. Listener(this); f. add. Mouse. Motion. Listener(this); both types of events cause methods to be called in the Two. Listen class. 6 -18

Two. Listen. java import java. awt. *; import java. awt. event. *; public class

Two. Listen. java import java. awt. *; import java. awt. event. *; public class Two. Listen implements Mouse. Motion. Listener, Mouse. Listener { private Text. Field tf; private Frame f; public Two. Listen () { f = new Frame("Two mouse listeners example"); f. add(new Label ("Click and drag the mouse"), Border. Layout. NORTH); tf = new Text. Field(30); f. add(tf, Border. Layout. SOUTH); f. add. Mouse. Motion. Listener(this); f. add. Mouse. Listener(this); f. set. Size(300, 200); Window. Destroyer listener = new Window. Destroyer(); // window destroy button f. add. Window. Listener(listener); f. set. Visible(true); } 6 -19

Two. Listen. java public void mouse. Dragged(Mouse. Event e) { String s = "Mouse

Two. Listen. java public void mouse. Dragged(Mouse. Event e) { String s = "Mouse dragging: X = " + e. get. X() + " Y = " + e. get. Y(); tf. set. Text(s); } public void mouse. Entered(Mouse. Event e) { String s = "The mouse Entered"; tf. set. Text(s); } public void mouse. Exited(Mouse. Event e) { String s = "The mouse has left the building"; tf. set. Text(s); } public void mouse. Moved(Mouse. Event e) {} public void mouse. Released(Mouse. Event e) {} public void mouse. Clicked(Mouse. Event e) {} public void mouse. Pressed(Mouse. Event e) {} public static void main(String args[]) { Two. Listen two = new Two. Listen(); } } 6 -20

AWT Components – – – – Button Checkbox Group: Radio Buttons Choice Canvas Text.

AWT Components – – – – Button Checkbox Group: Radio Buttons Choice Canvas Text. Field Text. Area List Dialog Scroll. Pane Menu Checkbox. Menu. Item Popup Menu Printing 6 -21

Button. Demo. java import java. awt. *; import java. awt. event. *; public class

Button. Demo. java import java. awt. *; import java. awt. event. *; public class Button. Demo implements Action. Listener { private Frame f; public Button. Demo () { f = new Frame("Button Example"); Button b = new Button("sample"); b. add. Action. Listener(this); f. add(b); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } public void action. Performed(Action. Event e) { System. out. println("Button press received. "); System. out. println("Button's action command is: " + e. get. Action. Command()); } public static void main(String args[]) { Button. Demo bd = new Button. Demo(); } } 6 -22

Check. Box. Demo. java import java. awt. *; import java. awt. event. *; public

Check. Box. Demo. java import java. awt. *; import java. awt. event. *; public class Checkbox. Demo implements Item. Listener { private Frame f; public Checkbox. Demo () { f = new Frame("Checkbox Example"); Checkbox one = new Checkbox("One", true); Checkbox two = new Checkbox("Two", false); Checkbox three = new Checkbox("Three", false); one. add. Item. Listener(this); two. add. Item. Listener(this); three. add. Item. Listener(this); f. set. Layout(new Flow. Layout()); f. add(one); f. add(two); f. add(three); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } 6 -23

Checkbox. Demo. java public void item. State. Changed(Item. Event e) { String state =

Checkbox. Demo. java public void item. State. Changed(Item. Event e) { String state = "deselected"; if (e. get. State. Change() == Item. Event. SELECTED) { state = "selected"; } System. out. println(e. get. Item() + " " + state); } public static void main(String args[]) { Checkbox. Demo bd = new Checkbox. Demo(); } } 6 -24

Checkbox. Group. Demo. java import java. awt. *; import java. awt. event. *; public

Checkbox. Group. Demo. java import java. awt. *; import java. awt. event. *; public class Checkbox. Group. Demo implements Item. Listener { private Frame f; public Checkbox. Group. Demo () { f = new Frame("Checkbox. Group Example"); Checkbox. Group cbg = new Checkbox. Group(); Checkbox one = new Checkbox("One", cbg, true); Checkbox two = new Checkbox("Two", cbg, false); Checkbox three = new Checkbox("Three", cbg, false); one. add. Item. Listener(this); two. add. Item. Listener(this); three. add. Item. Listener(this); f. set. Layout(new Flow. Layout()); f. add(one); f. add(two); f. add(three); f. pack(); 6 -25

Checkbox. Group. Demo. java Window. Destroyer listener = new Window. Destroyer(); // window destroyer

Checkbox. Group. Demo. java Window. Destroyer listener = new Window. Destroyer(); // window destroyer button f. add. Window. Listener(listener); f. set. Visible(true); } public void item. State. Changed(Item. Event e) { if (e. get. State. Change() == Item. Event. SELECTED) System. out. println(e. get. Item() + " Selected"); } public static void main(String args[]) { Checkbox. Group. Demo bd = new Checkbox. Group. Demo(); } } 6 -26

Choice. Demo. java import java. awt. *; import java. awt. event. *; public class

Choice. Demo. java import java. awt. *; import java. awt. event. *; public class Choice. Demo implements Item. Listener { private Frame f; public Choice. Demo () { f = new Frame("Choice Example"); Choice choice = new Choice(); choice. add. Item("First"); choice. add. Item("Second"); choice. add. Item("Third"); choice. add. Item. Listener(this); f. add(choice, Border. Layout. CENTER); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } public void item. State. Changed(Item. Event e) { if (e. get. State. Change() == Item. Event. SELECTED) System. out. println(e. get. Item() + " Selected"); } public static void main(String args[]) { Choice. Demo c = new Choice. Demo(); } } 6 -27

Canvas. Demo. java import java. awt. *; import java. awt. event. *; public class

Canvas. Demo. java import java. awt. *; import java. awt. event. *; public class Canvas. Demo extends Canvas implements Key. Listener { private int index; Color colors[] = {Color. red, Color. green, Color. blue}; public Canvas. Demo () { super(); } public void paint(Graphics g) { g. set. Color(colors[index]); g. fill. Rect(0, 0, get. Size(). width, get. Size(). height); } } public void key. Typed(Key. Event e) { index++; if (index == colors. length) index = 0; repaint(); } 6 -28

Canvas. Demo. java public void key. Pressed(Key. Event e) {} public void key. Released(Key.

Canvas. Demo. java public void key. Pressed(Key. Event e) {} public void key. Released(Key. Event e){} } public static void main(String args[]) { Frame f = new Frame("Canvas Example"); Canvas. Demo can = new Canvas. Demo(); can. set. Size(150, 150); f. add(can, Border. Layout. CENTER); can. request. Focus(); can. add. Key. Listener(can); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } 6 -29

Text. Area Text. Components: Text. Area and Text. Field 6 -30

Text. Area Text. Components: Text. Area and Text. Field 6 -30

6 -31

6 -31

Text. Area 6 -32

Text. Area 6 -32

Text. Area. Demo. java import java. awt. *; import java. awt. event. *; public

Text. Area. Demo. java import java. awt. *; import java. awt. event. *; public class Text. Area. Demo { private Frame f; private Text. Area tf; } public Text. Area. Demo () { f = new Frame("Text. Area"); tf = new Text. Area("Welcome to Sognag!", 5, 30); tf. add. Key. Listener(new Name. Handler()); f. add(tf, Border. Layout. CENTER); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); 6 -33

Text. Area. Demo. java class Name. Handler extends Key. Adapter { public void key.

Text. Area. Demo. java class Name. Handler extends Key. Adapter { public void key. Pressed(Key. Event e) { char c = e. get. Key. Char(); if (Character. is. Digit(c)) e. consume(); } } public void key. Typed(Key. Event e) {} public void key. Released(Key. Event e){} public static void main(String args[]) { Text. Area. Demo tfd = new Text. Area. Demo(); } } 6 -34

Text. Field 6 -35

Text. Field 6 -35

Text. Field 6 -36

Text. Field 6 -36

Text. Field. Demo. java import java. awt. *; import java. awt. event. *; public

Text. Field. Demo. java import java. awt. *; import java. awt. event. *; public class Text. Field. Demo { private Frame f; private Text. Field tf; } public Text. Field. Demo () { f = new Frame("Text. Field"); tf = new Text. Field("Single Line", 30); tf. add. Key. Listener(new Name. Handler()); f. add(tf, Border. Layout. CENTER); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); 6 -37

Text. Field. Demo. java class Name. Handler extends Key. Adapter { public void key.

Text. Field. Demo. java class Name. Handler extends Key. Adapter { public void key. Pressed(Key. Event e) { char c = e. get. Key. Char(); if (Character. is. Digit(c)) e. consume(); } } public void key. Typed(Key. Event e) {} public void key. Released(Key. Event e){} } public static void main(String args[]) { Text. Field. Demo tfd = new Text. Field. Demo(); } 6 -38

6 -39

6 -39

List 6 -40

List 6 -40

List 6 -41

List 6 -41

List 6 -42

List 6 -42

List. Demo. java import java. awt. *; import java. awt. event. *; public class

List. Demo. java import java. awt. *; import java. awt. event. *; public class List. Demo { private Frame f; private List l; public List. Demo () { f = new Frame("List"); l = new List(4, true); l. add("Welcome"); l. add("to"); l. add("Sogang"); l. add("University"); f. add(l, Border. Layout. CENTER); f. pack(); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } public static void main(String args[]) { List. Demo tfd = new List. Demo(); } } 6 -43

Dialog public class Dialog extends Window – A Dialog is a top-level window with

Dialog public class Dialog extends Window – A Dialog is a top-level window with a title and a border that is typically used to take some form of input from the user. The size of the dialog includes any area designated for the border. The dimensions of the border area can be obtained using the get. Insets method, however, since these dimensions are platformdependent, a valid insets value cannot be obtained until the dialog is made displayable by either calling pack or show. Since the border area is included in the overall size of the dialog, the border effectively obscures a portion of the dialog, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets. left, insets. top), and has a size of width - (insets. left + insets. right) by height - (insets. top + insets. bottom). – The default layout for a dialog is Border. Layout. – A dialog must have either a frame or another dialog defined as its owner when it's constructed. When the owner window of a visible dialog is hidden or minimized, the dialog will automatically be hidden from the user. When the owner window is subsequently re-opened, then the dialog is made visible to the user again. – A dialog can be either modeless (the default) or modal. A modal dialog is one which blocks input to all other toplevel windows in the app context, except for any windows created with the dialog as their owner. Dialogs are capable of generating the following window events: Window. Opened, Window. Closing, Window. Closed, Window. Activated, Window. Deactivated. 6 -44

Dialog box – All of our user interface components have appeared inside a frame

Dialog box – All of our user interface components have appeared inside a frame window that was created in the application. This is the most common situation if you write applets that run inside a Web browser. But if you write applications, you usually want separate dialogs to pop up to get information from the user. – Dialog is a Frame with components format: Dialog(Frame parent, Boolean modal) Dialog(Frame parent, String title, Boolean modal) class: java. awt. Dialog function: constructor (creates a dialog object) Modal dialog: You need information from the user before you can proceed ex) Reading file needs file name before open it. Modeless dialog: User can enter information in both the dialog and the remainder of the application. ex) tool bar 6 -45

Dialog format: dispose() class: java. awt. Window() function: Dispose (eliminate) the window. – How

Dialog format: dispose() class: java. awt. Window() function: Dispose (eliminate) the window. – How to use Dialog • Create the Dialog object • Place components in the Dialog box • Process with action. Performed() saek. Dlg = new Dialog(this, “프레임 바탕색 설정”, false); pal. Chk = new Checkbox(“빨강”); cho. Chk = new Checkbox(“초록”); pa. Chk = new Checkbox(“파랑”); saek. Dlg. add(new Button(“확인”)); saek. Dlg. add(pal. Chk); saek. Dlg. add(cho. Chk); saek. Dlg. add(pa. Chk); 6 -46 Checkbox and Button are added in the Dialog

6 -47

6 -47

6 -48

6 -48

Dialog. Demo. java import java. awt. *; import java. awt. event. *; public class

Dialog. Demo. java import java. awt. *; import java. awt. event. *; public class Dialog. Demo implements Action. Listener { private Frame f; private Dialog saek. Dlg; private Button pal, cho, pa, done; public Dialog. Demo() { f = new Frame("Dialog Test"); saek. Dlg = new Dialog(f, "프레임 바탕색 설정", false); pal = new Button("빨강"); saek. Dlg. add(pal); cho = new Button("초록"); saek. Dlg. add(cho); pa = new Button("파랑"); saek. Dlg. add(pa); done = new Button("확인"); saek. Dlg. add(done); pal. add. Action. Listener(this); cho. add. Action. Listener(this); pa. add. Action. Listener(this); done. add. Action. Listener(this); } } 6 -49 // dialog box saek. Dlg const.

Dialog. Demo. java saek. Dlg. set. Layout(new Flow. Layout(Flow. Layout. RIGHT)); saek. Dlg. set.

Dialog. Demo. java saek. Dlg. set. Layout(new Flow. Layout(Flow. Layout. RIGHT)); saek. Dlg. set. Background(Color. light. Gray); // background change saek. Dlg. pack(); f. set. Size(200, 200); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); saek. Dlg. set. Visible(true); } public void action. Performed(Action. Event e) { int r=0, g=0, b=0; if (e. get. Action. Command() == "빨강") r=255; if (e. get. Action. Command() == "초록") g=255; if (e. get. Action. Command() == "파랑") b=255; if(e. get. Action. Command() == "확인") saek. Dlg. dispose(); f. set. Background(new Color(r, g, b)); // background change f. repaint(); // frame renewal } public static void main(String[] args) { Dialog. Demo dd = new Dialog. Demo(); // frame construction } } 6 -50

File. Dialog 6 -51

File. Dialog 6 -51

File. Dialog 6 -52

File. Dialog 6 -52

File. Dialog 6 -53

File. Dialog 6 -53

FDialog. Demo. java import java. awt. *; public class FDialog. Demo extends Frame {

FDialog. Demo. java import java. awt. *; public class FDialog. Demo extends Frame { Image img; boolean hdae; String fname; public FDialog. Demo(String str) { super(str); set. Background(Color. white); File. Dialog f. Dlg = new File. Dialog(this, "이미지파일", File. Dialog. LOAD); f. Dlg. set. Visible(true); fname = f. Dlg. get. Directory() + f. Dlg. get. File(); img = Toolkit. get. Default. Toolkit(). get. Image(fname); } } 6 -54

FDialog. Demo. java public void paint(Graphics g) { if (img != null) g. draw.

FDialog. Demo. java public void paint(Graphics g) { if (img != null) g. draw. Image(img, 100, this); } public static void main(String[] args) { FDialog. Demo frm = new FDialog. Demo("파일 대화상자"); //frame construction frm. set. Size(500, 300); // frame size : 500*300(pixel) frm. set. Visible(true); Window. Destroyer listener = new Window. Destroyer(); frm. add. Window. Listener(listener); } } 6 -55

Scrollbar format: Scrollbar(int orient, int value, int min, int max) class: java. awt. Scrollbar

Scrollbar format: Scrollbar(int orient, int value, int min, int max) class: java. awt. Scrollbar constructor method that creates a scrollbar orient: Scrollbar. HORIZONTAL, Scrollbar. VERTICAL Event. UNIT_INCREMENT value: initial position of scroll bar Event. BLOCK_INCREMENT min, max: range of scrollbar (default size is 0~100) Event. TRACK format: int get. Value() int set. Value() class: java. awt. Scrollbar Gets (Sets) the position of the scroll bar Block increment Unit increment void set. Block. Increment(int format: void set. Unit. Increment(int n) n) class: java. awt. Scrollbar Sets the amount of increment (decrement) per the event 6 -56

Scroll. Bar. Demo. java import java. awt. *; import java. awt. event. *; public

Scroll. Bar. Demo. java import java. awt. *; import java. awt. event. *; public class Scroll. Bar. Demo implements Adjustment. Listener { private Frame f; private Scrollbar r. Scr, g. Scr, b. Scr; private Label r. Lbl, g. Lbl, b. Lbl; public Scroll. Bar. Demo() { f = new Frame("Scroll. Bar Test"); Panel p = new Panel(); p. set. Layout(new Grid. Layout(2, 3, 5, 0)); // partition 2 * 3 f. set. Size(500, 300); f. add("North", p); // arrange panel p to the north of frame p. add(r. Lbl = new Label("빨강")); // arrange label in panel p p. add(g. Lbl = new Label("초록")); p. add(b. Lbl = new Label("파랑")); // arrange Scrollbar in p panel p. add(r. Scr = new Scrollbar(Scrollbar. HORIZONTAL, 50, 0, 0, 255)); p. add(g. Scr = new Scrollbar(Scrollbar. HORIZONTAL, 100, 0, 0, 255)); p. add(b. Scr = new Scrollbar(Scrollbar. HORIZONTAL, 200, 0, 0, 255)); } 6 -57

Scroll. Bar. Demo. java r. Scr. add. Adjustment. Listener(this); g. Scr. add. Adjustment. Listener(this);

Scroll. Bar. Demo. java r. Scr. add. Adjustment. Listener(this); g. Scr. add. Adjustment. Listener(this); b. Scr. add. Adjustment. Listener(this); // increase/decrease amount when you press arrow button in scroll bar r. Scr. set. Block. Increment(30); g. Scr. set. Block. Increment(30); b. Scr. set. Block. Increment(30); // setting background of scrollbar r. Scr. set. Background(Color. red); g. Scr. set. Background(Color. green); b. Scr. set. Background(Color. blue); } Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); 6 -58

Scroll. Bar. Demo. java public void adjustment. Value. Changed(Adjustment. Event e) { if (

Scroll. Bar. Demo. java public void adjustment. Value. Changed(Adjustment. Event e) { if ( e. get. Adjustment. Type() == e. UNIT_INCREMENT || e. get. Adjustment. Type() == e. BLOCK_INCREMENT || e. get. Adjustment. Type() == e. UNIT_DECREMENT || e. get. Adjustment. Type() == e. BLOCK_DECREMENT || e. get. Adjustment. Type() == e. TRACK) { int r = r. Scr. get. Value(); // value of bar in scroll bar int g = g. Scr. get. Value(); int b = b. Scr. get. Value(); f. set. Background(new Color(r, g, b)); // background change r. Lbl. set. Text("빨강 : " + r); g. Lbl. set. Text("초록 : " + g); b. Lbl. set. Text("파랑 : " + b); } } } f. repaint(); // for change of label value // repainting public static void main(String[] args) { Scroll. Bar. Demo sb = new Scroll. Bar. Demo(); } 6 -59

Menu – Menu bar : sits on top of the window and contains the

Menu – Menu bar : sits on top of the window and contains the name of the pulldown menus – menu items : When the user clicks on a menu item, all menus are closed and a message is sent to the program. Adding menus is straightforward. You create a menu bar. Menu. Bar mb = new Menu. Bar(); For each menu, you create a menu object. Menu edit. Menu = new Menu(“Edit”); You add menu items, separators, and submenus to the menu object. edit. Menu. add(new Menu. Item(“Copy”)); edit. Menu. add. Separator(); edit. Menu. add(new Menu. Item(“Paste”)); 6 -60

Menu. Demo. java import java. awt. *; import java. awt. event. *; public class

Menu. Demo. java import java. awt. *; import java. awt. event. *; public class Menu. Demo implements Action. Listener { private Frame f; Color saek; public Menu. Demo() { f = new Frame("MENU TEST"); Menu. Bar jumenu = new Menu. Bar(); Menu m 1 = new Menu("색깔", true); // create menu 색깔 Menu. Item m 11 = new Menu. Item("빨강"); Menu. Item m 12 = new Menu. Item("파랑"); Menu. Item m 13 = new Menu. Item("초록"); Menu. Item m 14 = new Menu. Item("종료"); m 1. add(m 11); // addition in menu m 1. add(m 12); m 1. add(m 13); m 1. add. Separator(); // menu separation line m 1. add(m 14); jumenu. add(m 1); // addition of menu m 1 in jumenu } Menu m 2 = new Menu("모양"); // create menu 모양 Menu. Item m 21 = new Menu. Item("사각형"); Menu. Item m 22 = new Menu. Item("육면체"); m 2. add(m 21); m 2. add(m 22); jumenu. add(m 2); // addition of menu m 2 in jumenu 6 -61

Menu. Demo. java m 11. add. Action. Listener(this); m 12. add. Action. Listener(this); m

Menu. Demo. java m 11. add. Action. Listener(this); m 12. add. Action. Listener(this); m 13. add. Action. Listener(this); m 14. add. Action. Listener(this); m 21. add. Action. Listener(this); m 22. add. Action. Listener(this); f. set. Menu. Bar(jumenu); // set. Menu. Bar in frame f. set. Background(Color. white); // frame background color : white f. set. Size(200, 200); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } } public void action. Performed(Action. Event e) { if (e. get. Action. Command(). equals("빨강")) saek = Color. red; else if (e. get. Action. Command(). equals("초록")) saek = Color. green; else if (e. get. Action. Command(). equals("파랑")) saek = Color. blue; else if (e. get. Action. Command(). equals("종료")) System. exit(0); f. set. Background(saek); f. repaint(); } public static void main(String[] args) { Menu. Demo md = new Menu. Demo(); } 6 -62

Checkbox Menu : You can choose more then one item with checkbox menu. import

Checkbox Menu : You can choose more then one item with checkbox menu. import java. awt. *; public class mn. Check extends Frame { Checkbox. Menu. Item pal, cho, pa; public mn. Check() { set. Title("설정/해제 메뉴"); Menu. Bar jumenu = new Menu. Bar(); Menu m = new Menu("바탕색"); pal = new Checkbox. Menu. Item("빨강"); m. add(pal); cho = new Checkbox. Menu. Item("초록"); m. add(cho); pa = new Checkbox. Menu. Item("파랑"); m. add(pa); m. add. Separator(); m. add(new Menu. Item("종료")); jumenu. add(m); 6 -63

CMenu. Demo. java import java. awt. *; import java. awt. event. *; public class

CMenu. Demo. java import java. awt. *; import java. awt. event. *; public class CMenu. Demo implements Action. Listener, Item. Listener { private Frame f; Color saek; Checkbox. Menu. Item pal, cho, pa; public CMenu. Demo() { f = new Frame("Checkbox Menu TEST"); Menu. Bar jumenu = new Menu. Bar(); Menu m 1 = new Menu("색깔", true); pal = new Checkbox. Menu. Item("빨강"); pa = new Checkbox. Menu. Item("파랑"); cho = new Checkbox. Menu. Item("초록"); Menu. Item m 14 = new Menu. Item("종료"); m 1. add(pal); // addition in menu m 1. add(pa); m 1. add(cho); m 1. add. Separator(); // menu separation line m 1. add(m 14); jumenu. add(m 1); // addition of menu m 1 in jumenu Menu m 2 = new Menu("모양"); // create menu 모양 Menu. Item m 21 = new Menu. Item("사각형"); Menu. Item m 22 = new Menu. Item("육면체"); m 2. add(m 21); m 2. add(m 22); jumenu. add(m 2); // addition of menu m 2 in jumenu 6 -64

CMenu. Demo. java pal. add. Item. Listener(this); pa. add. Item. Listener(this); cho. add. Item.

CMenu. Demo. java pal. add. Item. Listener(this); pa. add. Item. Listener(this); cho. add. Item. Listener(this); m 14. add. Action. Listener(this); m 21. add. Action. Listener(this); m 22. add. Action. Listener(this); f. set. Menu. Bar(jumenu); // set. Menu. Bar in frame f. set. Background(Color. white); // frame background color : white f. set. Size(200, 200); Window. Destroyer listener = new Window. Destroyer(); f. add. Window. Listener(listener); f. set. Visible(true); } public void item. State. Changed(Item. Event e) { int r=0, g=0, b=0; if (pal. get. State()) r=255; // by each menu set if (cho. get. State()) g=255; if (pa. get. State()) b=255; f. set. Background(new Color(r, g, b)); // change of background color f. repaint(); } public void action. Performed(Action. Event e) { if (e. get. Action. Command(). equals("종료")) System. exit(0); } 6 -65

Printing – Allow the use of local printer conventions: Frame f = new Frame(“Print

Printing – Allow the use of local printer conventions: Frame f = new Frame(“Print Test”); f. set. Visible(true); Toolkit t = f. get. Toolkit(); Print. Job job = t. get. Print. Job(f, “My. Print. Job”, null); Graphics g = job. get. Graphics(); When users start a print operation, they see a printer selection dialog box. They can then choose options, such as paper size, print quality, and which printer to use. f. print. Components(g); The print() method asks a component to draw itself in this way. Submit that page to the printer g. dispose(); End the job and release the printer. job. end(); 6 -66

Print. Demo. java import java. awt. *; public class Print. Demo extends Frame {

Print. Demo. java import java. awt. *; public class Print. Demo extends Frame { public Print. Demo(String str) { super(str); } public static void main(String[] args) { Print. Demo f = new Print. Demo("Print Test"); f. set. Visible(true); Toolkit t = f. get. Toolkit(); Print. Job job = t. get. Print. Job(f, "My. Print. Job", null); Graphics g = job. get. Graphics(); } } 6 -67