Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004
Serial and Parallel Port Access Java Communications API –javax.comm Extension Package –
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: 1.public class MyClass implements ActionListener { 2.Registering the event handler someComponent.addActionListener(instanceOfMyClass); 3.An Event handler method public void actionPerformed(ActionEvent e) {...//code that reacts to the action... }
Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener