Object-Oriented Software Engineering Java with added Swing.
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Object-Oriented Software Engineering Java with added Swing.
22UniS
Contents
• Overview of Swing components
• Introduction to case study example
• Basic structure of Java Swing application
• Overview of runtime execution of application
33UniS
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/
Top-Level Containers
Applet
Dialog
Frame
66UniS
Swing Basics
Basic Controls
Buttons
Combo box
List
Menu
Slider
Spinner
Text field or Formatted text field
88UniS
Swing Basics
Interactive Displays of Highly Formatted Information
Color chooser
File chooser
Table
Text
Tree
99UniS
The JComponent Class
java.lang.Object java.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
1212UniS
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.
1313UniS
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);
1414UniS
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.
1515UniS
Swing Basics: Hello World Example
JFrame JLabel
String used in constructor method for JLabel
Exit program on close
1616UniS
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(); }}); }}
1717UniS
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
1818UniS
Sequence Diagram for HelloWorldSwing:HelloWorldSwing :Event Dispatcher frame:JFrame
label:JLabelframeCt:Container
createAndShowGUI
<<creates>>
setDefaultCloseOperation
<<creates>>
getContentPane
add(label)
pack
setVisible(true)
1919UniS
HelloWorldString Variable ScopeTrying 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.javaHelloWorldSwing.java:29: cannot resolve symbolsymbol : variable framelocation: class HelloWorldSwing java.awt.Container ct = frame.getContentPane(); ^1 error
2020UniS
String Handling in HelloWorldSwing
static String display_string =
"<html><H1>Hello World<P>"+ "<H1>This is an example of a"+ " <font color=#ff0a0a>JFrame</font> Swing Object.<BR>"+ "A <font color=#ff0a0a>JFrame</font>"+ " by itself does not provide any "+ "functionality.<BR>"+ "Even the text in the content pane of this"+ " <font color=#ff0a0a>JFrame</font><BR>"+ "is constructed by a <font color=#ff0a0a>JLabel</font> object."+ "</html>";
2121UniS
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 ";
2222UniS
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 = "<html><H1>Hello World<P>";
Note this is not always true.