1 / 95

Advanced Java Workshop

Advanced Java Workshop. John Cole Senior Lecturer Computer Science Department The University of Texas at Dallas August 23, 2014 http://www.utdallas.edu/~John.Cole. Topics. Programming Environments Introduction to classes Collection classes and generics: Set, List, Queue, Vector, Map

Télécharger la présentation

Advanced Java Workshop

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. Advanced Java Workshop John Cole Senior Lecturer Computer Science Department The University of Texas at Dallas August 23, 2014 http://www.utdallas.edu/~John.Cole Advanced Java Workshop

  2. Topics • Programming Environments • Introduction to classes • Collection classes and generics: Set, List, Queue, Vector, Map • Basic Swing: JFrame, Panels, Labels, Buttons, Text Fields, Combo Boxes, Lists • Event-driven programming • Text and Binary I/O • Multithreading Advanced Java Workshop

  3. NetBeans • URL: https://netbeans.org/downloads/7.4/ • Great general-purpose environment for most Java development • UTD doesn’t support 8.0 yet because of java issues, but 7.4 is solid and will do everything you want Advanced Java Workshop

  4. Eclipse with ADT • http://developer.android.com/sdk/index.html#download • While you can install the Android Developer Tools in NetBeans, I have had trouble with this • Eclipse is a great general-purpose java environment as well as having the Android tools • Get this if you’re taking CS6301: User Interface Design Advanced Java Workshop

  5. Other Resources • Both NetBeans and Eclipse have IntelliSense, but you may need more than that. • Java Docs: http://docs.oracle.com/javase/7/docs/api/ • Java Tutorials: http://docs.oracle.com/javase/tutorial/ Advanced Java Workshop

  6. Classes • There are no header files in Java. • The import statement finds packages that your program can use. • When you create a class, the declaration looks like this: public class Rectangle { private double length; private double width; } Advanced Java Workshop

  7. Classes • Any methods go in the braces. You don’t need to use prototypes: public class Rectangle { private double length; private double width; Public double getArea() { return length * width; } } Advanced Java Workshop

  8. Class Layout Conventions • The suggested organization of a source code file can vary by employer or instructor. • A common organization is: • Fields listed first • Methods listed second • Accessors and mutators are typically grouped. • There are tools that can help in formatting code to specific standards. Advanced Java Workshop

  9. Derived Classes • You can derive a new class from an existing one. A common thing to see in a GUI program is: public class MyFrame extends Jframe • You can then write your own constructor and other methods while using everything in JFrame Advanced Java Workshop

  10. Interfaces • An interface in java is like an abstract class • You must provide concrete implementations of all methods • The keyword for using an interface is implements • You can implement multiple interfaces public class c implements ActionListener Advanced Java Workshop

  11. Arrays • An array in Java is not just a set of memory locations; it is an actual object • You create an array the way you would any object: int[] numbers = new int[6]; • Note that you can have a variable in place of 6, since this is being allocated dynamically Advanced Java Workshop

  12. Two-Dimensional Arrays • A 2D array is essentially an array of arrays. Thus you can have: String[][] puzzle; puzzle = new String[5][5]; puzzle[0][0] = “search”; Advanced Java Workshop

  13. Ragged Arrays • Since a multidimensional array is an array of arrays, not all rows need contain the same number of columns: int[][] triangle = { {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {10}}; Advanced Java Workshop

  14. Collection Classes • ArrayList • HashSet • LinkedHashSet • TreeSet • Vector • Stack • Queue • HashMap Advanced Java Workshop

  15. ArrayList • If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection Advanced Java Workshop

  16. HashSet • The HashSet class is a concrete class that implements Set. It can be used to store duplicate-free elements. For efficiency, objects added to a hash set need to implement the hashCode method in a manner that properly disperses the hash code. Advanced Java Workshop

  17. LinkedHashSet • The elements in the HashSet are not ordered • LinkedHashSet elements can be retrieved in the order in which they were inserted Advanced Java Workshop

  18. TreeSet • SortedSet is a subinterface of Set, which guarantees that the elements in the set are sorted. • TreeSetis a concrete class that implements the SortedSet interface. • You can use an iterator to traverse the elements in the sorted order. • The elements can be sorted in two ways. Advanced Java Workshop

  19. TreeSet (Continued) • The elements can be sorted in two ways: • One way is to use the Comparable interface. • The other way is to specify a comparator for the elements in the set if the class for the elements does not implement the Comparable interface, or you don’t want to use the compareTo method in the class that implements the Comparableinterface Advanced Java Workshop

  20. Vector • In Java 2, Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. • None of the new collection data structures introduced so far are synchronized (thread-safe) Advanced Java Workshop

  21. Stack • The Stack class represents a last-in-first-out stack of objects • The elements are accessed only from the top of the stack. • You can retrieve, insert, or remove an element from the top of the stack Advanced Java Workshop

  22. Queue • A queue is a first-in/first-out data structure • Elements are appended to the end of the queue and are removed from the beginning of the queue Advanced Java Workshop

  23. HashMap • Efficient for locating a value, inserting a mapping, and deleting a mapping • Not sorted Advanced Java Workshop

  24. Generics • You can specify the data types of the elements of a collection. HashMap h<String, double[]> • This tells the compiler that the HashMap’s key will be a string and its data will be an array of doubles • This is checked at compile time only. Advanced Java Workshop

  25. Data Structure Exercise • Write a program that asks for the names and ages of 5 people. Put the information into a HashMap with the name as the key and the age as the data. • Do the same thing using a TreeMap • Print the data from each structure using an iterator Advanced Java Workshop

  26. Basic Swing • import javax.swing.* • Swing components are “lightweight” and very portable. • They don’t depend upon the underlying operating system • Don’t use the older AWT (Abstract Windowing Toolkit) controls unless absolutely necessary Advanced Java Workshop

  27. JFrame • When writing a GUI program, create a class derived from JFrame. • This lets you do initialization in the constructor • You can create controls that will show in the window, for example Advanced Java Workshop

  28. JFrame • Useful things in the constructor: this.setTitle(("Graduate Java Workshop")); this.setSize(500, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); Advanced Java Workshop

  29. Layout Managers • JFrames and other containers can have a layout that defines how contained graphical objects appear • This implies that you can have nested containers, each with its own layout • This was done because Java was intended to be independent of screen resolution Advanced Java Workshop

  30. FlowLayout • This is the simplest layout manager. Components are placed left to right, wrapping around at the edge of the container • You can change the way the components are aligned within the flow with constants: CENTER, LEFT, and RIGHT. Advanced Java Workshop

  31. FlowLayout Write a program that adds three labels and three text fields into the content pane of a frame with a FlowLayout manager. Advanced Java Workshop

  32. The FlowLayout Class Advanced Java Workshop

  33. GridLayout • Controls are arranged in a grid. You specify the number of rows and columns. frame.setLayout(new GridLayout(3,5)); • One peculiarity is that everything in a grid cell is sized to fit the cell. Advanced Java Workshop

  34. GridLayout This example shows using a GridLayout with 3 rows and 2 columns to display the labels and text fields. Advanced Java Workshop

  35. The GridLayout Class Advanced Java Workshop

  36. BorderLayout add(Component, constraint), where constraint is BorderLayout.EAST, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.NORTH, or BorderLayout.CENTER. The BorderLayout manager divides the container into five areas: East, South, West, North, and Center. Components are added to a BorderLayout by using the add method. Advanced Java Workshop

  37. BorderLayout Advanced Java Workshop

  38. The BorderLayout Class Advanced Java Workshop

  39. Panels A JPanel is a container that can be put into another container. • Panels have their own layout JpanelpnlTop = new JPanel(); pnlTop.setLayout(new GridLayout(4,3)); frame.add(pnlTop); • Note that the panel will be added to the frame’s layout. Advanced Java Workshop

  40. Controls • The screen objects you see, such as text fields, labels, checkboxes, buttons, and the like are collectively referred to as controls. Advanced Java Workshop

  41. JLabel • This is used to display text or an image, or both • The constructors for labels are as follows: JLabel() JLabel(String text, inthorizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, inthorizontalAlignment) JLabel(String text, Icon icon, inthorizontalAlignment) Advanced Java Workshop

  42. JTextField A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description). Advanced Java Workshop

  43. JTextField Constructors • JTextField(int columns) Creates an empty text field with the specified number of columns. • JTextField(String text) Creates a text field initialized with the specified text. • JTextField(String text, int columns) Creates a text field initialized with thespecified text and the column size. Advanced Java Workshop

  44. JTextField Properties • text • horizontalAlignment • editable • columns Advanced Java Workshop

  45. JTextField Methods • getText() Returns the string from the text field. • setText(String text) Puts the given string in the text field. • setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. • setColumns(int) Sets the number of columns in this text field.The length of the text field is changeable. Advanced Java Workshop

  46. JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text. Advanced Java Workshop

  47. JTextArea Constructors • JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns. • JTextArea(String s, int rows, int columns) Creates a text area with the initial text andthe number of rows and columns specified. Advanced Java Workshop

  48. JTextArea Properties • text • editable • columns • lineWrap • wrapStyleWord • rows • lineCount • tabSize Advanced Java Workshop

  49. JComboBox A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. Advanced Java Workshop

  50. JComboBox Methods To add an item to a JComboBoxjcbo, use jcbo.addItem(Object item) To get an item from JComboBoxjcbo, use jcbo.getItem() To get the index of an item, use jcbo.getItemIndex() Advanced Java Workshop

More Related