Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously … Polymorphism

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

jgs Test Yourselves Polymorphism

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

jgs Note Java Programming for Assignment 01

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

jgs Graphical User Interfaces GUI

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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); } }

Slide 17

Slide 17 text

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); } }

Slide 18

Slide 18 text

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); } }

Slide 19

Slide 19 text

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); } }

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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.