Download presentation
Presentation is loading. Please wait.
Published byElisabeth Gray Modified over 9 years ago
1
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004
2
Serial and Parallel Port Access Java Communications API –javax.comm Extension Package –http://java.sun.com/products/javacomm/index.jsp
3
Graphical User Interfaces (GUI) in Java Two sets of classes –AWT (Abstract Windowing Toolkit) –Swing
4
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
5
The Good News AWT and Swing are very similar So changing code from one to the other is not too difficult.
6
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
7
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.
8
Component Components that can contain other components are Containers. JFrame, JPanel and JWindow are examples of Containers JButton, JMenu are not containers
9
Top-Level Containers A Java GUI has at least one top-level container. The most common are: –JFrame –JDialog –JApplet
10
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);
11
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);
12
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);
13
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);
14
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);
15
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);
16
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); …
17
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); }
18
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... }
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.