Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Software Engineering Java with added Swing.

Similar presentations


Presentation on theme: "Object-Oriented Software Engineering Java with added Swing."— Presentation transcript:

1 Object-Oriented Software Engineering Java with added Swing

2 UniS 2 Contents Overview of Swing components Introduction to case study example Basic structure of Java Swing application Overview of runtime execution of application

3 UniS 3 Swing Basics Java provides a collection of Foundation classes to allow easy implementation of GUI-s (Graphical User Interface) For this lecture borrowed heavily from: http://java.sun.com/docs/books/tutorial/information/ http://java.sun.com/docs/books/tutorial/information/ Top-Level Containers Applet Dialog Frame

4 UniS 4 Swing Basics General-Purpose Containers Panel Scroll pane Split pane Tabbed pane Tool bar

5 UniS 5 Swing Basics Special-Purpose Containers Internal frame Layered pane Root pane

6 UniS 6 Swing Basics Basic Controls Buttons Combo box List Menu Slider Spinner Text field or Formatted text field Text fieldFormatted text field

7 UniS 7 Swing Basics Uneditable Information Displays Label Progress bar Tool tip

8 UniS 8 Swing Basics Interactive Displays of Highly Formatted Information Color chooser File chooser Table Text Tree

9 UniS 9 The JComponent Class java.lang.Objectjava.awt.Component java.awt.Container javax.swing.JComponent java.awt.Window java.awt.Frame javax.swing.JFrame javax.swing.JLabel javax.swing.JToolbar javax.swing.JPopupMenu javax.swing.JFileChooser javax.swing.JTextArea

10 UniS 10 Swing Basics: Course Case Study JTextAreaJTextField JToolBar JPopupMenu JFrame

11 UniS 11 Swing Basics: Course Case Study ImageIconJLabelJFileChooser

12 UniS 12 Swing Basics To make any graphic program work we must be able to create windows and add content to them. To make this happen we must: Import the pertinent packages. Set up a top-level container. Display the container. Be thread-safe.

13 UniS 13 Swing Basics Import the pertinent packages. import javax.swing.*; Set up a top-level container. JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("HelloWorldSwing"); Display the container. frame.pack(); frame.setVisible(true);

14 UniS 14 Swing Basics Be thread-safe. public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } } ); }; Swing event-handling and painting code executes in a single thread, called the event-dispatching thread. invokeLater adds a new thread to be executed by the event dispatcher.

15 UniS 15 Swing Basics: Hello World Example JFrame JLabel String used in constructor method for JLabel Exit program on close

16 UniS 16 import javax.swing.*; public class HelloWorldSwing { private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String display_string = “Some Text” JLabel label = new JLabel(display_string); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); }}); }}

17 UniS 17 HelloWorldSwing JLabel label = new JLabel(display_string); frame.getContentPane().add(label); JLabel label = new JLabel(display_string); java.awt.Container frameCt = frame.getContentPane(); frameCt.add(label); Strange Part of Code Is equivalent to these two lines

18 UniS 18 Sequence Diagram for HelloWorldSwing :HelloWorldSwing :Event Dispatcherframe:JFrame label:JLabel frameCt :Container createAndShowGUI > setDefaultCloseOperation > getContentPane add(label) pack setVisible(true)

19 UniS 19 HelloWorldString Variable Scope Trying to access variables declared inside createAndShowGUI outside of the definition is illegal. E.g: public class HelloWorldSwing {........ private static void createAndShowGUI() {........ JFrame frame = new JFrame("HelloWorldSwing");........ } java.awt.Container ct = frame.getContentPane(); public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } $ javac HelloWorldSwing.java HelloWorldSwing.java:29: cannot resolve symbol symbol : variable frame location: class HelloWorldSwing java.awt.Container ct = frame.getContentPane(); ^ 1 error

20 UniS 20 String Handling in HelloWorldSwing static String display_string = " Hello World "+ " This is an example of a"+ " JFrame Swing Object. "+ "A JFrame "+ " by itself does not provide any "+ "functionality. "+ "Even the text in the content pane of this"+ " JFrame "+ "is constructed by a JLabel object."+ " ";

21 UniS 21 String Handling in HelloWorldSwing String simpleString = "this" + "and" "that"; Is the same as: String simpleString = "thisandthat"; To get spaces would write: String simpleString = "this " + "and " + "that ";

22 UniS 22 String Handling in HelloWorldSwing Can add formatting such as new line `\n' and tabs `\t' String simpleString = "\nNewLine\twith tabs" Some swing components (such as JLabel) can interpret some HTML String simpleString = " Hello World "; Note this is not always true.

23 UniS 23

24 UniS 24


Download ppt "Object-Oriented Software Engineering Java with added Swing."

Similar presentations


Ads by Google