190 likes | 411 Views
Java GUI. CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004. Serial and Parallel Port Access. Java Communications API javax.comm Extension Package http://java.sun.com/products/javacomm/index.jsp. Graphical User Interfaces (GUI) in Java. Two sets of classes
E N D
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004
Serial and Parallel Port Access • Java Communications API • javax.comm Extension Package • http://java.sun.com/products/javacomm/index.jsp
Graphical User Interfaces (GUI) in Java • Two sets of classes • AWT (Abstract Windowing Toolkit) • Swing
AWT vs Swing • Mixing AWT & Swing is bad! • Advantages of Swing • Faster • More complete • Being improved • Advantages of AWT • Supported on older browsers • Java Micro-Edition (phones, PDAs, etc.) uses AWT, not Swing
The Good News • AWT and Swing are very similar • So changing code from one to the other is not too difficult.
A Note on Inheritance • A Java class can be derived from another class. • Suppose class B is derived from class A • We say B extends A • B is a subclass of A • B inherits features from A • An object of type B can be treated as if it is an object of type A
Swing • Each GUI feature in Swing (windows, buttons, dialog boxes, menus, scroll bars, etc.) is a component. • It is helpful to note that components in Swing are derived from components in AWT.
Component • Components that can contain other components are Containers. • JFrame, JPanel and JWindow are examples of Containers • JButton, JMenu are not containers
Top-Level Containers • A Java GUI has at least one top-level container. • The most common are: • JFrame • JDialog • JApplet
Our first Swing GUI (JFrame) // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
setSize // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); frame.setVisible(true);
Labels // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); // create a text label JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setVisible(true);
Buttons // create and set up the main window JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 100); // create a text label JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); JButton button = new JButton("I'm a Swing button!"); frame.getContentPane().add(button); frame.setVisible(true);
Panels and Layouts JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello World"); JButton button = new JButton("I'm a Swing button!"); // create a panel to hold the button and the label JPanel panel = new JPanel(new GridLayout(0,1)); // add the button and label to the panel panel.add(button); panel.add(label); // then put the panel in the frame frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);
Borders // create a panel to hold the button and the label JPanel panel = new JPanel(new GridLayout(0,1)); panel.setBorder(BorderFactory.createEmptyBorder( 30, //top 30, //left 10, //bottom 30) //right ); // add the button and label to the panel panel.add(button); panel.add(label); // then put the panel in the frame frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);
Events public class swingExample7 implements ActionListener { … public static void main(String[] args) { swingExample7 mainObj = new swingExample7(); … // create a button JButton button = new JButton("I'm a Swing button!"); button.addActionListener(mainObj); …
Events public class swingExample7 implements ActionListener { static JLabel label; static String labelPrefix = "Number of clicks: "; static int numClicks = 0; public static void main(String[] args) { … } public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } }
Event Handlers in General • Require 3 pieces of code: • public class MyClass implements ActionListener { • Registering the event handler someComponent.addActionListener(instanceOfMyClass); • An Event handler method public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }