1 / 19

Java GUI

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

miya
Download Presentation

Java GUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: • 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... }

  19. Listeners

More Related