1 / 16

JFC and Swing JFC is short for Java Foundation Classes JFC contains a group of features for

JFC and Swing JFC is short for Java Foundation Classes JFC contains a group of features for 1. building graphical user interfaces (GUIs) 2. adding rich graphics functionality and interactivity to Java applications.

swain
Télécharger la présentation

JFC and Swing JFC is short for Java Foundation Classes JFC contains a group of features for

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. JFC and Swing JFC is short for Java Foundation Classes JFC contains a group of features for 1. building graphical user interfaces (GUIs) 2. adding rich graphics functionality and interactivity to Java applications. "Swing" refers to the package names for the Swing API which begin with javax.swing

  2. import javax.swing.*; public class HelloWorldSwing { private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

  3. Every program with a Swing GUI must have at least one top-level Swing container. Commonly used top-level Swing containers: JFrame - implements a single main window JDialog - implements a secondary window (a window dependent on another window) JApplet - implements an applet’s display area within a browser window. JFrame frame = new JFrame("HelloWorldSwing");

  4. JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("HelloWorldSwing"); ... frame.pack(); frame.setVisible(true);

  5. HelloWorldSwing uses a JComponent descendant called JLabel, which displays the text Hello World. These two lines of code construct and then add the JLabel component to the frame: • JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); • label is added to the frame’s content pane instead of to the frame itself. • Every top-level container has a content pane that contains, directly or indirectly, all the visible components (except for menus and window decorations) in the top-level container.

  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); In older programs the above code is replaced by: frame.addWindowListener(newWindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });

  7. Handling Events Every time the user types a character or pushes a mouse button, an event occurs. Any object can be notified of the event. All the object has to do is implement the appropriate interface and be registered as an event listener on the appropriate event source. • Every event handler requires three pieces of code: • In the declaration for the event handler class, one line of code specifies that the class either implements a listener interface or extends a class that implements a listener interface. • public class MyClass implements ActionListener { • 2. Another line of code registers an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); • The event handler class has code that implements the methods in the listener interface. For example: • public void actionPerformed(Action Event e) { ...//code that reacts to the action... }

  8. public class SwingApplication implements ActionListener { ... JButton button = new JButton("I'm a Swing button!"); button.addActionListener(this); .... public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } }

  9. The JComponent Class The JComponent Class With the exception of top-level containers, all Swing components whose names begin with "J" descend from the JComponent class. For example, JPanel, JScrollPane, JButton, and JTable all inherit features and methods from JComponent. JFrame and JDialog don't because they implement top-level containers.

  10. The JComponent Class Customizing Component Appearance

  11. The JComponent Class Setting and Getting Component State

  12. The JComponent Class Handling Events (Writing Event Listeners)

  13. JButton

  14. Setting or Getting the Button's Contents

More Related