120 likes | 144 Views
This lecture covers class dependencies in software systems and introduces GUI components such as frames, panels, labels, and buttons in Java Swing. Examples and demonstrations are provided.
E N D
CS110- Lecture 12Mar 23, 2005 • Agenda • Questions on Project 2? • Class dependencies • Components and Containers CS110-Spring 2005, Lecture 12
Class dependencies • The classes in a software system have various types of relationships to each other. • We have seen many examples of “dependency” relationship in which one class uses another i.e. one class relies on another in some sense. • Often the methods of one class invokes the methods of another class. • Example: • ChaosGame is dependent on Die class • MTSO class is dependent on Tower class • Tower class is dependent on Location class CS110-Spring 2005, Lecture 12
Class Dependencies • Sometimes a class depends on itself i.e. an object of one class interacts with another object of the same class. • Example • Location class is dependent on itself because distance method needs another Location object. Header of distance method is: public double distance(Location other) { } CS110-Spring 2005, Lecture 12
Class dependencies • Lets write a class ComplexNumber. • Instance data: • Real part • Imaginary part • Methods: • sum(ComplexNumber other) • prod(ComplexNumber other) • neg() • Various Setters and Getters CS110-Spring 2005, Lecture 12
Components and Containers (Section 3.9) • We have used java capabilities to draw shapes using the Graphics and Color classes from standard library. • We wrote applets to draw shapes. • But we can also write applications (stand – alone programs) with graphical components. • We need main method to run such applications. CS110-Spring 2005, Lecture 12
Components and Containers • A GUI component is an object that represents a screen element that is used to display information or allow the user to interact with the program in a certain way. • GUI components include labels, buttons, text fields, radio buttons, check boxes and so on…. CS110-Spring 2005, Lecture 12
Components and Containers • GUI components and other GUI related classes are primarily in two packages: • AWT – Abstract windowing toolkit (old) • Components- Button, Label etc. • Other Classes – Color etc. • Swing (New) • Components- JButton, JLabel etc. • Other classes – Timer etc. • We will still use some classes of AWT such as Color. • But for components we will prefer using Swing Components. CS110-Spring 2005, Lecture 12
Components and Containers • A Container is a special type of Component that is used to hold and organize other components. • AWT • Containers – Frame, Panel etc. • Swing • Containers – JFrame, JPanel etc. CS110-Spring 2005, Lecture 12
Frames • Frame is a container that is used to display GUI based Java applications. • Frame is displayed as a separate window with its own title bar. • In can be resized, moved, maximized and minimized. • In swing Frame is defined by the JFrame class. CS110-Spring 2005, Lecture 12
Panels • Panel is also a container. However it cannot be displayed at its own. A panel must be added to another container for it to be displayed. • Its primary role is to help organize the other components in a GUI. • In swing panel is defined by JPanel class. CS110-Spring 2005, Lecture 12
Some Fundamental GUI components • Label • Displays a line of text. • In swing label is defined by JLabel class. • Button • Allows user to click at them and we can associate an action when button is clicked • In swing button is defined by JButton class CS110-Spring 2005, Lecture 12
Frames and Panels • Lets write a class that gives us the following output. Swing Welcome to world of Swing Swing is Fun OK EXIT CS110-Spring 2005, Lecture 12