lesson 5 miscellaneous language features swing programming

47
Lesson 5 Miscellaneous language features Swing Programming

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 5 Miscellaneous language features Swing Programming

Lesson 5

Miscellaneous language features

Swing Programming

Page 2: Lesson 5 Miscellaneous language features Swing Programming

Assertions

• Use the assert statement to insert assertions at particular points in the code. The assert statement can have one of two forms: – assert booleanExpression; – assert booleanExpression : errorMessage;  

• The errorMessage is an optional string message that would be shown when an assertion fails.

Page 3: Lesson 5 Miscellaneous language features Swing Programming

Assertions

Example: Simple check for rogue values

Class Foo{ public void foo(int x[], int n){ assert n > 0 && n < x.length; “n out of range”; … }}

Example: Simple check for null objectclass Foo{ public void foo(){ Student s = StudentFactory.create(“Graduate”); assert s != null; }}

Page 4: Lesson 5 Miscellaneous language features Swing Programming

Assertions

• Assertions are not typically used to signal errors in production client application code.

• Rather, they are used – During development– By library writers to signals errors to library users (who

themselves are software developers!)

• java –ea | -da Program – (“enable assertions” or “disable assertions”)

Page 5: Lesson 5 Miscellaneous language features Swing Programming

Assertions

• prompt> java AssertionTest3 • and you enter a valid character, it will work

fine. However, if you enter an invalid character, nothing will happen. This is because, by default, assertions are disabled at runtime. To enable assertions, use the switch -enableassertion (or -ea) as follows:

• prompt> java -ea AssertionTest3prompt> java -enableassertion AssertionTest3

Page 6: Lesson 5 Miscellaneous language features Swing Programming

Foreach loop

// Returns the sum of the elements of a int sum(int[] a) { int result = 0; for (int i : a) result += i; return result; }

//Compare to old wayint sum(int[] a) { int result = 0; for (int i = 0; i < a.length; ++i) result += a[i]; return result; }

Much cleaner syntax!Think “for each i in the Collection a”

Page 7: Lesson 5 Miscellaneous language features Swing Programming

ForEach loop

• Also used to replaced iterators in more general collections (Lists, HashMaps, etc.)

• More on this when we cover collections

Page 8: Lesson 5 Miscellaneous language features Swing Programming

Varargs

• Allows you to define/call methods with variable number of arguments.

• Before– void foo(int i, String[] args) … was called as– String args[] = {“hello”, “goodbye”}; foo(1, args);

• As of 1.5 it can also be called more flexibly as– void foo(int i, String… args)– foo(1,”hello”, “goodbye”);

Page 9: Lesson 5 Miscellaneous language features Swing Programming

Varargs

• Note that the vararg parameter, denoted by the ellipses (“…”), must always be the last one.

• Inside the method it is always handled as an array of whatever size.

• However, it can be called as a method with any number of arguments without having to box them in an array!

Page 10: Lesson 5 Miscellaneous language features Swing Programming

enumerations

Page 11: Lesson 5 Miscellaneous language features Swing Programming

Static import

• In order to access static members, it is necessary to qualify references with the class they came from. For example– Math.cos(Math.PI * theta);

• Now, can avoid this with:– import static java.lang.Math.PI;

• Once the static members have been imported, they may be used without qualification: – double r = cos(PI * theta);

Page 12: Lesson 5 Miscellaneous language features Swing Programming

Typesafe Enums

• Old way:– public static final int SEASON_WINTER = 0;

– public static final int SEASON_SPRING = 1;

– public static final int SEASON_SUMMER = 2;

– public static final int SEASON_FALL = 3;

• New way:– enum Season {WINTER, SPRING, SUMMER, FALL };

Page 13: Lesson 5 Miscellaneous language features Swing Programming

New way

• Season now declares a new type that can only have the specified values!– Enhanced compiler-time checking possible– Also declares a namespace– In old way printed values are uninformative

Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.

Page 14: Lesson 5 Miscellaneous language features Swing Programming

More than meets the eye

• Enumerated types also have many other cool high-level features that make them better than C enums– They can be looped over as collections– They can be looped over in ranges– They can be cloned– They can be printed

• See Enum.java in class examples

Page 15: Lesson 5 Miscellaneous language features Swing Programming

Adding methods to enum types

public enum Season{ SUMMER, SPRING, FALL, WINTER;

public String clothing(){ if (this == SUMMER) return("short sleeves"); else if (this == SPRING) return("Winter coat"); else if (this == FALL) return("long sleeves"); else if (this == WINTER) return("two winter coats"); return(null); }}

Note that enumsCan be declared In their own classesscc

For enums, “this” refersto the value of the operatingEnumerated type (ie SUMMER,SPRING, etc.)

Page 16: Lesson 5 Miscellaneous language features Swing Programming

More on enum

• enum has a toString method that will give the String representation of the value of this

> Season s = Season.WINTER;

> String sval = s.toString();

> System.out.pritnln(sval)

> WINTER

Page 17: Lesson 5 Miscellaneous language features Swing Programming

C-style printing

• If you like C-style printing that is now available as:

• import java.io.*;

• public class PrintTest{• public static void main(String[] args){• float x = 1.2f;• PrintStream p = new PrintStream(System.out);• p.printf("%s: %f\n", "the result is", x);

• }• }

Page 18: Lesson 5 Miscellaneous language features Swing Programming

Swing

Page 19: Lesson 5 Miscellaneous language features Swing Programming

Swing Components

• Swing is a collection of libraries that contains primitive widgets or controls used for designing Graphical User Interfaces (GUIs).

• Commonly used classes in javax.swing package:– JButton, JTextBox, JTextArea, JPanel, JFrame, JMenu,

JSlider, JLabel, JIcon, …– There are many, many such classes to do anything

imaginable with GUIs– Here we only study the basic architecture and do simple

examples

Page 20: Lesson 5 Miscellaneous language features Swing Programming

Swing components, cont.

• Each component is a Java class with a fairly extensive inheritency hierarchy:

Object

Component

Container

JComponent

JPanel

Window

Frame

JFrame

Page 21: Lesson 5 Miscellaneous language features Swing Programming

Using Swing Components

• Very simple, just create object from appropriate class – examples:– JButton but = new JButton();– JTextField text = new JTextField();– JTextArea text = new JTextArea();– JLabel lab = new JLabel();

• Many more classes. Don’t need to know every one to get started.

• See ch. 9 Hortsmann

Page 22: Lesson 5 Miscellaneous language features Swing Programming

Adding components

• Once a component is created, it can be added to a container by calling the container’s add method:

Container cp = getContentPane();

cp.add(new JButton(“cancel”));

cp.add(new JButton(“go”));

How these are laid out is determined by the layout manager.

This is required

Page 23: Lesson 5 Miscellaneous language features Swing Programming

Laying out components

• Not so difficult but takes a little practice

• Do not use absolute positioning – not very portable, does not resize well, etc.

Page 24: Lesson 5 Miscellaneous language features Swing Programming

Laying out components

• Use layout managers – basically tells form how to align components when they’re added.

• Each Container has a layout manager associated with it.

• A JPanel is a Container – to have different layout managers associated with different parts of a form, tile with JPanels and set the desired layout manager for each JPanel, then add components directly to panels.

Page 25: Lesson 5 Miscellaneous language features Swing Programming

Layout Managers

• Java comes with 7 or 8. Most common and easiest to use are– FlowLayout– BorderLayout– GridLayout

• Using just these three it is possible to attain fairly precise layout for most simple applications.

Page 26: Lesson 5 Miscellaneous language features Swing Programming

Setting layout managers

• Very easy to associate a layout manager with a component. Simply call the setLayout method on the Container:

JPanel p1 = new JPanel();p1.setLayout(new FlowLayout(FlowLayout.LEFT));

JPanel p2 = new JPanel();p2.setLayout(new BorderLayout());

As Components are added to the container, the layout manager determines their size and positioning.

Page 27: Lesson 5 Miscellaneous language features Swing Programming

Event handling

Page 28: Lesson 5 Miscellaneous language features Swing Programming

What are events?

• All components can listen for one or more events.• Typical examples are:

– Mouse movements

– Mouse clicks

– Hitting any key

– Hitting return key

– etc.

• Telling the GUI what to do when a particular event occurs is the role of the event handler.

Page 29: Lesson 5 Miscellaneous language features Swing Programming

ActionEvent

• In Java, most components have a special event called an ActionEvent.

• This is loosely speaking the most common or canonical event for that component.

• A good example is a click for a button.• To have any component listen for

ActionEvents, you must register the component with an ActionListener. e.g.– button.addActionListener(new MyAL());

Page 30: Lesson 5 Miscellaneous language features Swing Programming

Delegation, cont.

• This is referred to as the Delegation Model.• When you register an ActionListener with a

component, you must pass it the class which will handle the event – that is, do the work when the event is triggered.

• For an ActionEvent, this class must implement the ActionListener interface.

• This is simple a way of guaranteeing that the actionPerformed method is defined.

Page 31: Lesson 5 Miscellaneous language features Swing Programming

actionPerformed

• The actionPerformed method has the following signature:void actionPerformed(ActionEvent)

• The object of type ActionEvent passed to the event handler is used to query information about the event.

• Some common methods are:– getSource()

• object reference to component generating event

– getActionCommand()• some text associated with event (text on button, etc).

Page 32: Lesson 5 Miscellaneous language features Swing Programming

actionPerformed, cont.

• These methods are particularly useful when using one eventhandler for multiple components.

Page 33: Lesson 5 Miscellaneous language features Swing Programming
Page 34: Lesson 5 Miscellaneous language features Swing Programming
Page 35: Lesson 5 Miscellaneous language features Swing Programming
Page 36: Lesson 5 Miscellaneous language features Swing Programming
Page 37: Lesson 5 Miscellaneous language features Swing Programming
Page 38: Lesson 5 Miscellaneous language features Swing Programming
Page 39: Lesson 5 Miscellaneous language features Swing Programming

Simplest GUI

import javax.swing.JFrame;class SimpleGUI extends JFrame{

SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE);

show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Page 40: Lesson 5 Miscellaneous language features Swing Programming

Another Simple GUIimport javax.swing.*;class SimpleGUI extends JFrame{

SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”);

Container cp = getContentPane();//must do this cp.add(but1);

show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Page 41: Lesson 5 Miscellaneous language features Swing Programming

Add Layout Managerimport javax.swing.*; import java.awt.*;class SimpleGUI extends JFrame{

SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”);

Container cp = getContentPane();//must do this cp.setLayout(new FlowLayout(FlowLayout.CENTER);

cp.add(but1); show();

} public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Page 42: Lesson 5 Miscellaneous language features Swing Programming

Add call to event handlerimport javax.swing.*; import java.awt.*;class SimpleGUI extends JFrame{

SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”);

Container cp = getContentPane();//must do this cp.setLayout(new FlowLayout(FlowLayout.CENTER);

but1.addActionListener(new MyActionListener()); cp.add(but1); show();

} public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Page 43: Lesson 5 Miscellaneous language features Swing Programming

Event Handler Codeclass MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(“I got clicked”, null); }

}

Page 44: Lesson 5 Miscellaneous language features Swing Programming

Add second button/event

class SimpleGUI extends JFrame{SimpleGUI(){

/* .... */ JButton but1 = new JButton(“Click me”);

JButton but2 = new JButton(“exit”); MyActionListener al = new MyActionListener();

but1.addActionListener(al); but2.addActionListener(al); cp.add(but1);

cp.add(but2); show();

}}

Page 45: Lesson 5 Miscellaneous language features Swing Programming

How to distinguish events –Less good way

class MyActionListener implents ActionListener{ public void actionPerformed(ActionEvent ae){ if (ae.getActionCommand().equals(“Exit”){

System.exit(1); } else if (ae.getActionCommand().equals(“Click me”){ JOptionPane.showMessageDialog(null, “I’m clicked”); }

}

Page 46: Lesson 5 Miscellaneous language features Swing Programming

Good wayclass MyActionListener implents ActionListener{ public void actionPerformed(ActionEvent ae){ if (ae.getSource() == but2){

System.exit(1); } else if (ae.getSource() == but1){ JOptionPane.showMessageDialog(null, “I’m clicked”); }}

Question: How are but1, but2 brought into scope to do this?Question: Why is this better?

Page 47: Lesson 5 Miscellaneous language features Swing Programming

Putting it all together

• See LoginForm.java example in class notes