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