170 likes | 360 Views
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.
E N D
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
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(); } }); } }
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");
JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("HelloWorldSwing"); ... frame.pack(); frame.setVisible(true);
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.
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); } });
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... }
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); } }
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.
The JComponent Class Customizing Component Appearance
The JComponent Class Setting and Getting Component State
The JComponent Class Handling Events (Writing Event Listeners)