Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 10

CSE205 Lecture 10

Object-Oriented Programming and Data Structures
Java Swing
(202202)

Javier Gonzalez-Sanchez

September 20, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 205
    Object-Oriented Programming and
    Data Structures
    Lecture 10: Java Swing
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View full-size slide

  2. jgs
    Previously …
    Polymorphism

    View full-size slide

  3. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3
    jgs
    The Class Object
    § The Object class is a special class defined in the java.lang package.
    § All classes are derived from the Object class.

    View full-size slide

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4
    jgs
    Conversions

    View full-size slide

  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5
    jgs
    getClass

    View full-size slide

  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6
    jgs
    getClass

    View full-size slide

  7. jgs
    Test Yourselves
    Polymorphism

    View full-size slide

  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8
    jgs
    Test Yourselves

    View full-size slide

  9. jgs
    Note
    Java Programming for Assignment 01

    View full-size slide

  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10
    jgs
    The toString Method

    View full-size slide

  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11
    jgs
    The toString Method
    § The toString method is a method that takes no parameter and returns a
    string
    § A returned string usually contains information on instance variables of its
    class.
    § Each class has a default toString method that contains its class object
    name and hash number.
    § When an object is used with System.out.println method, its toString method
    will be called.

    View full-size slide

  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12
    jgs
    The toString Method

    View full-size slide

  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13
    jgs
    java.Util.Scanner

    View full-size slide

  14. jgs
    Graphical User Interfaces
    GUI

    View full-size slide

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15
    jgs
    Java API

    View full-size slide

  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16
    jgs
    A Frame
    import javax.swing.*;
    public class Example extends JFrame{
    public Example() {
    super("Hello World Swing");
    }
    public static void main(String[] args) {
    Example frame = new Example();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true); }
    }

    View full-size slide

  17. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17
    jgs
    Widgets
    import javax.swing.*;
    public class Example extends JFrame{
    public Example() {
    super("Hello World Swing");
    JLabel label = new JLabel("Hello World");
    add(label);
    }
    public static void main(String[] args) {
    Example frame = new Example();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true); }
    }

    View full-size slide

  18. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18
    jgs
    Widgets
    import javax.swing.*;
    public class Example extends JFrame{
    public Example() {
    super("Hello World Swing");
    FlowLayout layout = new FlowLayout();
    setLayout(layout);
    JLabel label = new JLabel("Hello World");
    add(label);
    JButton button = new JButton("Click Me");
    add(button);
    JTextField text = new JTextField("Text", 10);
    add(text);
    JCheckBox check = new JCheckBox("Option");
    add(check);
    }
    public static void main(String[] args) {
    Example frame = new Example();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true); }
    }

    View full-size slide

  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19
    jgs
    Layouts
    import javax.swing.*;
    public class Example extends JFrame{
    public Example() {
    super("Hello World Swing");
    GridLayout layout = new GridLayout(3,3);
    setLayout(layout);
    JLabel label = new JLabel("Hello World");
    add(label);
    JButton button = new JButton("Click Me");
    add(button);
    JTextField text = new JTextField("Text", 10);
    add(text);
    JCheckBox check = new JCheckBox("Option");
    add(check);
    }
    public static void main(String[] args) {
    Example frame = new Example();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true); }
    }

    View full-size slide

  20. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20
    jgs
    import javax.swing.*;
    public class Example extends JFrame{
    public Example() {
    super("Hello World Swing");
    BorderLayout layout = new BorderLayout();
    setLayout(layout);
    JLabel label = new JLabel("Hello World");
    add(label, BorderLayout.CENTER);
    JButton button = new JButton("Click Me");
    add(button , BorderLayout.SOUTH);
    JTextField text = new JTextField("Text", 10);
    add(text , BorderLayout.NORTH);
    JCheckBox check = new JCheckBox("Option");
    add(check , BorderLayout.EAST);
    }
    public static void main(String[] args) {
    Example frame = new Example();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true); }
    }
    Layouts

    View full-size slide

  21. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21
    jgs
    Questions

    View full-size slide

  22. jgs
    CSE 205 Object-Oriented Programming and Data Structures
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University.
    They cannot be distributed or used for another purpose.

    View full-size slide