Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Software Engineering JFrames with Swing.

Similar presentations


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

1 Object-Oriented Software Engineering JFrames with Swing

2 2 Contents JFrame Content Pane Layout Managers Java Interfaces for Swing ActionListeners

3 3 JFrame Any interesting GUI will use the JFrame class. public class FrameDemo { private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }

4 4 JFrame private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }

5 5 Content Pane The menu bar and content pane provide functionality for a JFrame.

6 6 JFrame Problem is createAndShowGUI is declared static: private static void createAndShowGUI() Static methods can not reference any non-static variables. This includes any reference to a non-static class. This has to be, because main has to be static, and can only reference static methods.

7 7 JFrame For example, move declaration of frame outside scope of createAndShowGUI: private JFrame frame; private static void createAndShowGUI() {............ frame = new JFrame("FrameDemo");............ } $ javac FrameDemo.java FrameDemo.java:10: non-static variable frame cannot be referenced from a static context frame = new JFrame("FrameDemo");

8 8 Content Pane, JPanel public class MyContents extends JPanel { public MyContents ( ) { // put contents of JFrame in this // non-static constructor method } private static void createAndShowGUI() {..................... JFrame frame = new JFrame("Person");..................... MyContents newContentPane = new MyContents( );..................... frame.setContentPane(newContentPane);..................... }

9 9 Layout Managers A JPanel must have a layout manager to control where buttons, icons, text areas etc are to be placed with respect to each other inside the content pane. newContentPane LayoutManager BorderLayout CardLayout FlowLayout GridLayout GridBagLayout JPanel { choice of } * * * * *

10 10 BorderLayout Every content pane is initialized to use a BorderLayout. BorderLayout has five areas. These areas are specified by the BorderLayout constants PAGE_START, PAGE_END, LINE_START, LINE_END, and CENTER.

11 11 Code for adding buttons to BorderLayout JButton button = new JButton("Button 1 (PAGE_START)"); newContentPane.add(button, BorderLayout.PAGE_START); //Make the center component big, since that's the //typical usage of BorderLayout. button = new JButton("Button 2 (CENTER)"); button.setPreferredSize(new Dimension(200, 100)); newContentPane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); newContentPane.add(button, BorderLayout.LINE_START); button = new JButton("Long-Named Button 4 (PAGE_END)"); newContentPane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); newContentPane.add(button, BorderLayout.LINE_END); Code taken from Java tutorial, also on course website.

12 12 BorderLayout: Note Behaviour of buttons depends on the object to which it belongs. In a later lecture we will see that buttons attached to JToolBar will seem to behave differently when the JToolBar is then attached to a BorderLayout within a JFrame!

13 13 BoxLayout BoxLayout stacks components either as column or in row.

14 14 BoxLayout Code public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); addAButton("Button 1", pane); addAButton("Button 2", pane); addAButton("Button 3", pane); addAButton("Long-Named Button 4", pane); addAButton("5", pane); } private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } Code taken from Java tutorial, also on course website.

15 15 GridBagLayout GridBagLayout aligns components by placing them within a grid of cells Components can span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.

16 16 GridBagLayout Grid (0,0)Grid (0,1)Grid (0,2) Grid (1,0) Grid (1,1) Grid (1,2) Grid (2,0)Grid (2,1)Grid (2,2)

17 17 GridBagLayout JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; Constraint controls where buttons appear within the GridBagLayout. Resizing component will cause horizontal space to be shared across buttons

18 18 GridBagLayout button = new JButton("Button 1"); c.gridx = 0; c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); c.gridx = 1; c.gridy = 0; pane.add(button, c); button = new JButton("Button 3"); c.gridx = 2; c.gridy = 0; pane.add(button, c); button = new JButton("Long-Named Button 4"); c.ipady = 40; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(button, c);

19 19 GridBagLayout button = new JButton("5"); c.ipady = 0; //reset to default c.weighty = 1.0; //request any extra vertical space c.anchor = GridBagConstraints.PAGE_END; //bottom of space c.insets = new Insets(10,0,0,0); //top padding c.gridx = 1; //aligned with button 2 c.gridwidth = 2; //2 columns wide c.gridy = 2; //third row pane.add(button, c);

20 20 Java Interfaces Interface represents a contract defining a protocol. An interface is a named collection of method definitions, without implementations. An interface can also declare constants. An interface cannot implement any methods. An interface has no constructor. All interface methods are public. No static methods allowed. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. A class can implement many interfaces but can have only one superclass.

21 21 MouseInputListener and ActionListener > MouseInputListener void mouseClicked (MouseEvent) void mouseMoved (MouseEvent) void mouseExited (MouseEvent) void mouseReleased (MouseEvent) void mouseEntered (MouseEvent) void mousePressed (MouseEvent) void mouseDragged (MouseEvent) void actionPerformed(ActionEvent) ActionListene r

22 22 Event Listeners For a class to provide interaction it might implement the ActionListener and/or the MouseInputListener interfaces in Java as well as extend JPanel. MyNewClas s «interface» ActionListene r JPane l «interface» MouseInputListene r JFrame

23 23 Handling Events GUI Components communicate with each other by sending events. A listener is an object that is notified when an event occurs and can examine the contents of that event to perform some action. Event Listener Object GUI Component Event fired by GUI Registers to listen for particular type of events

24 24 Handling Events GUI Component Any number of objects can register to listen for same type of event Events are multicast to all objects that are listening Even t

25 25 Common Events and their Listeners > java.util.EventListene r java.awt.AWTEvent MouseEven t ActionEven t > MouseListener > MouseMotionListene r > ActionListene r

26 26 Listening for Events, Quick Quiz Draw a sequence diagram to show this scenario: One object acts as a source of events: aSource One object listens for events: aListener aListener registers with the source to be notified when events occur by getting aSource to execute its register( ) method. When an event occurs, aSource creates anEvent object. Then aSource causes aListener to execute its notify(anEvent) method. Then aListener queries the anEvent object by getting it to execute its query( ) method.

27 27 Listening For Events aSource aListener register( ) anEvent > query( ) notify(anEvent ) (2 marks for life line) (6 marks for method) (6 marks for create) (2 marks for notify) (2 marks for query) Total 20 marks


Download ppt "Object-Oriented Software Engineering JFrames with Swing."

Similar presentations


Ads by Google