7 import javax.swing.*; public class Example extends JFrame{ public static void main(String[] args) { Example frame = new Example(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.setVisible(true); } } Method main()
8 import javax.swing.*; public class Example extends JFrame{ public Example() { super("Hello World Swing"); GridLayout layout = new GridLayout(3,2); 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
Winter 2022 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.