1 / 26

CIS3931 - Intro to JAVA

Learn how to create home and reload buttons for a web browser in Java GUI programming. Includes applets, spot forms, and an update on the final exam.

ctheisen
Download Presentation

CIS3931 - Intro to JAVA

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS3931 - Intro to JAVA Lecture Notes Set 14 21-July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets

  2. Spot Forms …

  3. Reminder…Update on the FINAL • The final will be entirely multiple choice • The lowest grade you can receive on the final and still pass the class is a 65 ! • The final will be approximately 30 questions (about 22-25 repeated questions from the previous midterms and 5-8 new questions) • There will be 4 or 5 extra credit questions, for a total of 20 available extra credit points

  4. Thursday’s class… • Turning a JAVA program into a web-viewable applet • A few more notes on program 6 • Notes on the “protected”, “private”, “public”, “static”, and “final” modifiers.

  5. Applets • The Applet class is part of the AWT. • A newer class called JApplet is included in Swing, but is not as commonly used as the Applet class. • HOWEVER : The JApplet class makes turning an existing GUI program into an applet VERY easy …

  6. JApplet • Instead of extending JFrame, you want to extend JApplet in your program. • JFrame and JApplet are very similar … so, almost everything will work without modification • There are a few exceptions …

  7. Example : Changing textaction.java into an Applet • textaction.java was discussed in the last class. • The first step is to take the textaction.java program and rename it to something like textactionapplet.java • Then --- Modify it so your public class extends JApplet instead of JFrame

  8. textactionapplet.java • If you try to compile the program at this point, you will get a few errors… thornton@program3:~/CIS3931/21-July-05>javac textactionapplet.java textactionapplet.java:20: cannot find symbol symbol : constructor JApplet(java.lang.String) location: class javax.swing.JApplet super ("TextAction Example"); ^ textaction.java:81: cannot find symbol symbol : method addWindowListener(<anonymous java.awt.event.WindowAdapter>) location: class textaction addWindowListener(new WindowAdapter( ) ^ 2 errors

  9. textactionapplet.java • The errors are as follows : • The JApplet class does’t have a super method associated with it … so, you have to remove that. • There is no window listener available with applets …

  10. textactionapplet.java • Since you aren’t extending your entire program from the JFrame class, you will have to create one somewhere else. • You will be modifying the “main” section of your program to create and customize the main JFrame.

  11. textactionapplet.java public static void main (String[] args) { //Create a JFrame here instead .... JFrame frame = new JFrame("Making an applet"); //Add the textactionapplet information to this frame frame.getContentPane().add(new textactionapplet()); //Set the JFrame size frame.setSize(300,300); //Set the JFrame to be visible frame.setVisible(true); }

  12. textactionapplet.java • Now, the program should compile without any errors. • Next, you will have to move the class files to your webserver. Make sure to set the appropriate file permissions on them. • Finally, you will need to create a webpage to display the applet.

  13. Webpage for the textactionapplet <HEAD> <BODY> <APPLET code=textactionapplet.class height=300 width=500> </APPLET> </BODY> </HEAD>

  14. The Applet Webpage • See http://www.cs.fsu.edu/~cis3931/applet.html • The applet should function just as the application did. • The .class file can still be run as an application from a command line using the normal “java” run command. • This program is now both and applet and an application.

  15. Public, Private, and Protected • The information needed to fully understand the use of these modifiers is a little beyond the scope of this class; however, it is important to at least understand at the basic level what each of these actually do. • These modifiers have a bearing on access privileges in programs that have multiple classes associated with them.

  16. Public, Private, and Protected • When used in a class declaration : Keywords used to specify the access levels for member variables and functions (methods). • When used on a method : Used to specify the access levels for the method and its variables. • When used on a variable : Used to specify the access level for the single variable.

  17. Public, Private, and Protected • Public : visible to all classes • Private : Visible only to the class to which it belongs • Protected : Visisble to the class to which they belong and to all subclasses of that class.

  18. Deciding when to use the modifiers • Think about whether or not an external object (or program) actually needs direct access to the information. • Really only applies in complex object oriented programming with multiple classes and methods.

  19. Example public class bank_balance { public String owner; public int balance; public bank_balance( String name, int dollars ) { owner = name; //Check to be sure the balance is positive if (dollars >= 0) balance = dollars; //If the balance is not positive, make it 0 else dollars = 0; } }

  20. Example • In this example, both the string and integer are public. • Any object in the system can change the balance (even to a negative value). • This could cause the program to fail even though there are checks in the bank_balance class to prevent negative values.

  21. Example : modified • The better way to make this program would be to prove two methods, getBalance and setBalance. • Also – the balance should be either private or protected. • Other objects can still access the data, but the can’t input invalid data.

  22. Example : modified public class bank_balance { public String owner; private int balance; public bank_balance( String name, int dollars ) { owner = name; if (dollars >= 0) balance = dollars; else dollars =0; } public int getBalance() { return balance; } public void setBalance(int dollars) { if (dollars >= 0) balance = dollars; else dollars = 0; } }

  23. The reload button • Your reload button should attempt to reload the page that is currently being displayed • It will be hard to tell if this is actually working because pages sometimes load very quickly • We will be checking your code to be sure you performed the reload correctly

  24. The reload button • The reload button first needs to determine the current page location • Your textfield should be showing the current page location (it should be updated each time you click on a link!!) • You will then simply call a setPage (or the function that loads your URLS) on the location

  25. The home button • The home button is loads a static page • For assignment 6, home should load http://www.google.com • Just call a setPage on the address (or use your function that loads the URL • Remember to update the text in the textfield as well! (When you click home, your textfield should say http://www.google.com)

  26. Questions ? • Any other topics to cover before the end of class? • Only one more lecture class left, Assignment 7 will be assigned • Next Thursday will be the review for the final • The final is the Tuesday of the last week of class • The last Thursday of class is intended to be used for help with Assignment 7 as well as to finalize grades and go over the grades on the final exam.

More Related