Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.

Similar presentations


Presentation on theme: "Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used."— Presentation transcript:

1 Ch13 Creating windows and applets

2 Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used graphic classes SWING: Is a part of The Java Foundation Classes (JFC) Extensive package for the creation of GUI’s AWT vs. Swing Many AWT components have improved Swing counterpartsMany AWT components have improved Swing counterparts For example, the AWT Button class corresponds to a more versatile Swing class called JButtonFor example, the AWT Button class corresponds to a more versatile Swing class called JButton

3 Swing Hierarchy

4 Swing Components Frame Combo box Button List Menu Dialog Text Fields

5 The Swing’s Event model In the new event model a component can initiate an event. Each type of event is represented by a distinct class. When an event is fired, it is received by one or more “listeners,” which act on that event.

6 Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) ItemEvent ItemListener itemStateChanged(ItemEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent)

7 JFrame 1. import javax.swing.*; 2. 3. public class MyFrame 4. { 5. public static void main(String[ ] args) 6. { 7. JFrame f = new JFrame(“My First Frame"); 8. f.setSize(400,300); 9. f.setVisible(true); 10. } 11. }

8 JFrame 1. import javax.swing.*; 2. import javax.awt.*; 3. import javax.awt.event.*; 4. public class FrameDemo 5. { 6. public static void main(String s[ ]) { 7. JFrame frame = new JFrame("FrameDemo"); 8. Container content = frame.getContentPane( ); 9. frame.addWindowListener(new WindowAdapter() { 10. public void windowClosing(WindowEvent e) { 11. System.exit(0);} 12. }); 13. frame.pack(); 14. frame.setVisible(true); 15. }

9 Closing Windows Steps to complete before a window will be able to close itself: Import the package java.awt.event.* so we get access to a windowlistener Add a windowlistener to the frame-object Implement the correct event for the listener, in this case the windowClosing event

10 Example: Closing-capability 1. import javax.swing.*; 2. import javax.awt.*; 3. import javax.awt.event.*; 4.public static void main(String[] args) { 5. MyFrame myWindow = new MyFrame(); 6. myWindow.addWindowListener(new WindowAdapter(){ 7. public void windowClosing(WindowEvent e) 8. { 9. System.exit(0); 10. }//windowClosing 11. }); 12. }

11 Example: Buttons //: Buttons.java // Various Swing buttons import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; import javax.swing.border.*; Import com.bruceeckel.swing.*; public class Buttons extends JApplet { JButton jb = new JButton("JButton"); BasicArrowButton up = new BasicArrowButton( BasicArrowButton.NORTH), down = new BasicArrowButton( BasicArrowButton.SOUTH), right = new BasicArrowButton( BasicArrowButton.EAST), left = new BasicArrowButton( BasicArrowButton.WEST);

12 public void init( ) { Container cp = get ContentPane( ); Cp.setLayout(new FlowLayout( ) ); add(jb); add(new JToggleButton("JToggleButton")); add(new JCheckBox("JCheckBox")); add(new JRadioButton("JRadioButton")); JPanel jp = new JPanel(); jp.setBorder(new TitledBorder("Directions")); jp.add(up); jp.add(down); jp.add(left); jp.add(right); add(jp); } public static void main(String args[]) { Console.run(new Buttons(), 300, 200); } } ///:~


Download ppt "Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used."

Similar presentations


Ads by Google