Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Windows, Networking and other Tidbits Chapter Fourteen.

Similar presentations


Presentation on theme: "1 Windows, Networking and other Tidbits Chapter Fourteen."— Presentation transcript:

1 1 Windows, Networking and other Tidbits Chapter Fourteen

2 2 Topics: l Windows, menus and dialog boxes –allow real pop up windows now from Java applets l Networking - load HTML from browser, retrieve files from web sites work with generic sockets l Extra stuff

3 3 Windows, menus and dialog boxes l Frames AWT Window class provides for windows that are independent (own titles, resize handels, menu bars) l Subclasses: l Frame - fully functioning window with menubar l Dialog - more limited

4 4 To create a frame l new Frame( ) // = no title l new Frame(String) has given title l Frames are containers “like panels” l default layout is BorderLayout –win = new Frame(“My Way Cool Window”); –win.setLayout(new Borderlayout(10,20)); –win.add(“North”, new Button(“Start”)); –win.add(“Center”, new Button(“Move”));

5 5 Sizes moves location show hide l resize( ) to set size l move( ) to set location l location( ) can tell the applet window is on screen l win.resize(100, 200); l Dimension d = location( ); l win.move(d.width + 50, d.height + 50);

6 6 l When you create a window it is invisable l show( ); // to make it appear l hide( ); // to make it disappear

7 7 A popup window 14-1 l public class GUI extends java.applet.Applet { Frame window; l public void init( ) { –add(new Button(“Open Window”)); –add(new Button(“Close Window”)); –window = new MyFrame(“A Popup Window”); –window.resize(150, 150); window.show( );

8 8 l public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { l String label = (String)arg; l if (label.equals(“Open Window”)) { l if (!window.isShowing( ) ) –window.show( );} l else if (label = = “Close Window”) { l if (window.isShowing( ) ) –window.hide( ); } l return true; }

9 9 List 14.1 contd. l else return false; } } l class MyFrame extends Frame { l Label l // lowercase L is variable l MyFarame(String title) { l Super(title); l setLayout(new GridLayout(1,1)); //ones l l = new Label(“This is a Window”, Label.center); add(l); } // l is lower L

10 10 Programming hint: Do Not use lowercase letter L’s they look too much like the digit One.... I keep telling publishers that and have done it myself - choose something else. Old time keypunch operators got sheets in the writers hand Z vs. 2, l vs. 1 This is not a new concept...

11 11 Menus and Menubars l To create: –MenuBar mb = new MenuBar( ); l Set as default: –window.setMenuBar(mb); l Add individual menus (File, Edit etc.) l menu m = new menu(“file”); l mb.add(m); // Some systems NOT All l // would allow mb.setHelpMenu(m);

12 12 Menu Items l Instances of the class MenuItem –Menu m = new Menu(“Tools”); m.add(new MenuItem(“Info”)); l Instances of the class CheckBoxMenuItem l Other menus with their own items l Seperators = lines that divide groups

13 13 Submenus l To create: l Menu sb = new Menu (“sizes”); l m.add(sb) // sub to m in previous slide l sb.add(new MenuItem(“Small”)); l medium and large etc. can also be added

14 14 CheckboxMenuItem l A menu item with a checkbox l CheckboxMenuItem coords = new CheckBoxMenuItem(“Show Coordinates”); l m.add(coords); l Again m from previous menu l Any menu can be enable( ) disable( )

15 15 Menu Actions l public boolean action(Event evt, Object arg) { if (evt.target instanceof MenuItem) { l String label = (String)arg; l if(label.equals(“Show Coordinates”)) toggleCoords( ); l else if (label.equals(“Fill”)) fillcurrentArea( ); return true; } else return false; }

16 16 l Add a menu: Window from class MyFrame l MyFrame(String title) { –Super(title); –MenuBar mb = new MenuBar( ); –Menu m = new Menu(“Colors”); –m.add(new MenuItem(“Red”)); –m.add(new MenuItem(“Blue”)); –m.add(new MenuItem(“Green”)); –m.add(new MenuItem(“-”)); –m.add(new CheckboxMenuItem(“Reverse Text”)); mb.add(m); mb.setHelpMenu(m); –setMenuBar(mb); //... }

17 17 l To run the last slide you need an action( ) method: l public boolean action(Event evt, Object arg) { String label = (String)arg; l if (evt.target instanceof MenuItem) { l if (label.equals (“Red”)) setBackground(Color.red); l else if (label.equals(“Blue”)) setBackgroundColor.blue); l else if (label.equals(“Green”)) setBackgroundColor.green); return true; }

18 18 l if (evt.target instanceof CheckboxMenuItem) { l if (getForeground( ) = = Color.balck) l setForeground(Color.white); l else setForeground(Color.black); l return true; } l return false; }

19 19 Dialog Boxes l transient windows - popup with warnings ask for info. etc. l Two types in AWT: –Dialog class = generic –FileDialog = platform specific (save/open)files l To create: l Dialog(Frame, boolean) initial invisable –true = modal false not modal

20 20 l Dialog(Frame, String, boolean) same as last slide BUT has titlebar and title l can be show( ) hide( ) just like Frames l To add: l m.add(new MenuItem(“Set Text...”)); l dl = new Dialog(this, “Enter Text”, true); l dl.setLAyout(new GridLayout(2, 1, 30, 30)); l tf = new TextField(l.getText( ), 20); l dl.add(tf); dl.add(new Button(“OK”)); l dl/resize(150, 75); // Choose OK = dismiss

21 21 File Dialogs - Can’t access or Severe restrictions on local system Really just for stand alone l To create: FileDialog (Frame, String) l FileDialog(Frame, String, int) // (load/save) –FileDialog.SAVE. OR FileDialog.LOAD l FIleDialog fd = new FileDialog(this, “FileDialog”); l fd.show( );

22 22 Window Events l WINDOW-DESTROY l WINDOW-EXPOSE // brought forward l WINDOW-ICONIFY l WINDOW-DEICONIFY l WINDOW-MOVED l Can test for all of these in the Event class

23 23 AWT in Stand Alone Applications l Can use all the applet stuff: l Can use Graphics etc.

24 24 Networking in Java l ShowDocument( ) // Load - link to other web page l openStream( ) open connect to URL l Socket classes: Socket and ServerSocket open standard socket connections (read/write to them)

25 25 Create Links inside Applets l URL class To create a new URL l URL(String, String, int, String) –http ftp gopher file –host name (www.lne.com, ftp.netcom.com) –a port number (80 for http) l URL(String, String, String) Same as above minus port number l URL(String) String should include “All”

26 26 Srting url = “http//www.yahoo.com)/”); l try ( theURL = new URL(url); { l catch (MAlformedURLException e) { –System.out.println(“Bad URL: “ + theURL); –} l After you have the URL Link it: l getAppletContext( ).showDocument(theURL):

27 27 Bookmark buttons l import java.awt.*; l import java.net.URL; l import java.net.MalformedURLExceptions; l public class ButtonLink extends java.applet.Applet { l Bookmark bmlist[] = new Bookmark[3];

28 28 l public void init( ) { l bmlist[0] = new Bookmark(“Laura’s Home Page”, “http://www.lne.com/lemay/”); l bmlist[1] = new Bookmark(“Yahoo”, “http://www.yahoo.com”); l bmlist[2] = new Bookmark(“Java Home Page”, “http://www.java.sun.com”); l setLayout(new Bookmark(“GridLayout(bmlist.length,1, 10,10)); for(int i=0; i< bmlist.length; i++); l add(new Button(bmlist[i].name)); } }

29 29 l public boolean action(Event evt, Object arg) { l if(evt.target instanceof Button) { l linkto((String) arg); l return true } l else return false; } l Void LinkTo(String name) { l URL theURL = null; for (int i=0; i < bmlist.length; i++) { l if (name.equals(bmlist[i].name)) theURL = bmlist[i].url; }

30 30 l if (theURL != null) getAppletContext( ).showDocument(theURL); } } l class Bookmark { String name; URL url; l Bookmark(String name, String theURL) { l this.name = name; l try { this URL = new URL(theURL); } l catch (MalformedURLException e) { l System.out.println(“Bad URL: “ + theURL); } } }

31 31 Opening Web Connections openStream( ) l To open a net connection - given URL l try { inputStream in = theURL.openStream( ); l DataInputStream data new DataInputStream((new BufferedInputStream(in); String line; l while (( line = data.readLine( )) != null) { l System.out.println(line); } }

32 32 l catch (IOException e) { l System.out.Pintln(“IO Error: “ + e.getMessage( )); }

33 33 The Get (Poe’s) Raven class l Page 293 - 294 complete text l Note the following: l All the imports - more than ever before (7) l public class GetRaven extends java.applet.Applet l implements Runnable { l URL theURL; Thread runner; l TextArea ta = new Text(“Getting text...”,30,70);

34 34 l public void init( ) { l String url = “http:/www.lne.com/Web/Java/raven.txt”; l try { this.theURL = new URL(url); } l catch (MalformedURLException e) { –System.out.println(“Bad URL: “ + theURL); –} add(ta); } l NOT going to review insets( ) –start( ) stop( )

35 35 l public void run( ) { InputStream conn; l data = new DataInputStream(new BufferedInputStream(conn)); l while((line = data.readLine( )) != null) { –buf.append(line + “\n”); } –ta.setText(buf.toString( )); } –catch (IOException e) { System.out.println(“IO Error:” + e.getMessage( )); } } }

36 36 URLconnection Class l openStream( ) simple for l URLconnection class.URLconnection –A way to retrieve files

37 37 Sockets l Java provides socket and ServerSocket l They provide for networking applications beyond what URL and URLconnection classes offer l Socket connection = new Socket(hostname, portnum);

38 38 l DataInputStream in = new DataINputStream( new BufferedINputStream(connection.getIn putStream( ))); l DataoutputStream out = new DataOutputStream( new BufferedOutputStream(connection.getO utputStream( ))); l connection.close( ) // when done & hide it l ServerSocket sconnection = new ServerSocket(8888); sconnection.accept( ); l See java.net for more info java sockets

39 39 Other Applet Hints l showStatus( ) // use print error etc. msgs: l getAppletContext( ).showStatus(“Change the color); // access browser features l Applet Information: –public String getAppletInfo( ) { –return “GetRaven copyright 1995 Laura Lemay;’ }

40 40 Communication Between Applets l for (Enumeration e = getAppletContext( ).getApplets( ); l e.hasMoreElements( );) { l Applet current = (Applet)(e.nextElement( )); l current.sendMessage } –//getApplets( ) returns Enumeration Object –A list of Applets on the page

41 41 l to call specific applet: l This applet sends information: l <APPLET CODE=“MyApplet.class” WIDTH=100 HEIGHT=150 l NAME = “sender””> l This applet receives information from the sender: l <APPLET CODE=“MyApplet.class” WIDTH=100 HEIGHT=150 l NMAE=“receiver”>

42 42 Finally: l //get ahold of the receiver applet l Applet receiver = getAppletContext( ).getApplet(“receiver”); l //tell it to update itself. –RECEIVER.UPDATE(TEXT, VALUE);


Download ppt "1 Windows, Networking and other Tidbits Chapter Fourteen."

Similar presentations


Ads by Google