DOC PREVIEW
Duke CPS 100E - Graphical User Interfaces GUIs

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Graphical User Interfaces GUIsThe PlanComponentsSlide 4Slide 5Flat LayoutsSlide 7Slide 8Slide 9Hierarchical LayoutsSlide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Designing a GUICoding a GUIExamplesBorderLayout.javaSlide 23Slide 24Slide 25Slide 26Event HandlingSlide 28Sequential (Single Thread) ModelEvent ModelSlide 31Slide 32Making the GUI InteractiveSlide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40PracticeGUI.1Graphical User InterfacesGUIsGUI.2The Plan•Components•Flat Layouts•Hierarchical Layouts•Designing a GUI•Coding a GUIGUI.3Components•JLabeltext/image display•JTextFieldsingle line for text input/output•JTextAreamultiple lines for text input/output•JButtonused for decisions•JFramea basic windowGUI.4Components•JLabeltext/image display•JTextFieldsingle line for text input/output•JTextAreamultiple lines for text input/output•JButtonused for decisions•JFramea basic windowGUI.5Components•JLabeltext/image display•JTextFieldsingle line for text input/output•JTextAreamultiple lines for text input/output•JButtonused for decisions•JFramea basic windowGUI.6Flat LayoutsGridLayout BorderLayoutCENTERNORTHSOUTHWESTEASTGUI.7Flat LayoutsGridLayout•Added left to right, top to bottom•Expands to fill horizontally and vertically•Each space equal width and heightBorderLayout•Not all positions must be filled•CENTER expands horizontally and vertically•NORTH and SOUTH expand horizontally•WEST and EAST expand verticallyGUI.8Flat LayoutsBorderLayoutGUI.9Flat LayoutsGridLayoutGUI.10Hierarchical LayoutsYou can put layouts within layouts:GUI.11Hierarchical LayoutsIdentify the BorderLayout and GridLayouts in the application on the right.GUI.12Hierarchical LayoutsCENTER EASTGUI.13Hierarchical LayoutsGridLayoutGUI.14Hierarchical LayoutsGridLayoutGUI.15Hierarchical LayoutsCENTERSOUTHGUI.16Hierarchical LayoutsCENTERSOUTHGUI.17Hierarchical LayoutsGUI.18Hierarchical Layouts•Virtually every layout I make is a hierarchy of GridLayout and BorderLayout•Other Layouts include–BoxLayout–GridBagLayout–FlowLayout–CardLayoutGUI.19Designing a GUI•What components are needed?•Which components are of primary importance? Secondary?•How do the components relate to each other?•How big are the components?•How can they be arranged into BorderLayout and GridLayout?GUI.20Coding a GUI1. Declare the components as instance variables2. Write a makeComponents method to initialize the components3. Write a layoutComponents methods to arrange the components4. Write a constructor to call the above two methods5. Write a setVisible method to set the primary component’s visibility (usually a JFrame).GUI.21Examples•BorderExample.java (today)•GridExample.java (in the code directory)•CombinedExample.java (in code directory)GUI.22BorderLayout.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class BorderExample extends JApplet{ JFrame frame; JTextArea middle; JTextField bottom; JButton left, right; JLabel title;GUI.23BorderLayout.java private void makeComponents() { frame=new JFrame("BorderExample"); middle=new JTextArea(10, 40); bottom=new JTextField(); left=new JButton("left"); right=new JButton("right"); title=new JLabel("Title"); }GUI.24BorderLayout.java private void makeLayout() { Container container=frame.getContentPane(); container.setLayout(new BorderLayout()); container.add(new JScrollPane(middle), BorderLayout.CENTER); container.add(title, BorderLayout.NORTH); container.add(left, BorderLayout.WEST); container.add(right, BorderLayout.EAST); container.add(bottom, BorderLayout.SOUTH); frame.pack(); }GUI.25BorderLayout.java public BorderExample() { makeComponents(); makeLayout(); } public void setVisible(boolean vis) { frame.setVisible(vis); }GUI.26BorderLayout.java public void init() { main(null); } public static void main(String[] args) { BorderExample example=new BorderExample(); example.setVisible(true); }GUI.27Event HandlingGUI.28The Plan•Sequential (Single Thread) Model•Event Model•Making the GUI interactive•Examples•PracticeGUI.29Sequential (Single Thread) ModelProgramStartProgramEndGUI.30Event ModelAWTEventLoopProgramThreadGUI.31Event ModelProgramThread•main is called•GUI is constructed•Thread terminatesGUI.32Event ModelAWTEventLoop•Check for input•Find event listeners•Notify listenersGUI.33Making the GUI Interactive1) import java.awt.event.*2) implements ActionListener3) write methodpublic void actionPerformed(ActionEvent e)4) call addActionListener(this) to all JButtonsGUI.34ExamplesAdderGUI.javaGameShell.javaGUI.35ExamplesAdderGUI.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class AdderGUI extends JApplet implements ActionListenerGUI.36ExamplesAdderGUI.java public void actionPerformed(ActionEvent ae) { String addend0Text=addend0.getText(); double addend0Number=Double.parseDouble(addend0Text); String addend1Text=addend1.getText(); double addend1Number=Double.parseDouble(addend1Text); double answer=addend0Number+addend1Number; sum.setText(""+answer); }GUI.37ExamplesAdderGUI.java private void makeComponents() { frame=new JFrame("Game Shell"); addend0=new JTextField(8); addend1=new JTextField(8); sum=new JTextField(8); compute=new JButton("="); compute.addActionListener(this); plus=new JLabel("+"); plus.setHorizontalAlignment(SwingConstants.CENTER); }GUI.38ExamplesGameShell.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class GameShell extends JApplet implements ActionListenerGUI.39ExamplesGameShell.java public void actionPerformed(ActionEvent ae) { Object cause=ae.getSource(); if(cause==pause) { if(pause.getText().equals("Pause")) { pause.setText("Resume"); shell.setText("Paused"); } else { pause.setText("Pause"); shell.setText("Game Running");


View Full Document

Duke CPS 100E - Graphical User Interfaces GUIs

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Graphical User Interfaces GUIs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Graphical User Interfaces GUIs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Graphical User Interfaces GUIs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?