1 / 12

Ch13 Creating windows and applets

Ch13 Creating windows and applets. Short overview. AWT (Abstract Windowing Toolkit) Early 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

derica
Télécharger la présentation

Ch13 Creating windows and applets

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. Ch13Creating windows and applets

  2. Short overview AWT (Abstract Windowing Toolkit) • Early 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 counterparts • For example, the AWT Button class corresponds to a more versatile Swing class called JButton

  3. Swing Hierarchy

  4. Swing Components Frame Combo boxButton 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 • import javax.swing.*; • public class MyFrame • { • public static void main(String[ ] args) • { • JFrame f = new JFrame(“My First Frame"); • f.setSize(400,300); • f.setVisible(true); • } • }

  8. JFrame • import javax.swing.*; • import javax.awt.*; • import javax.awt.event.*; • public class FrameDemo • { • public static void main(String s[ ]) { • JFrame frame = new JFrame("FrameDemo"); • Container content = frame.getContentPane( ); • frame.addWindowListener(new WindowAdapter() { • public void windowClosing(WindowEvent e) { • System.exit(0);} • }); • frame.pack(); • frame.setVisible(true); • }

  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 • import javax.swing.*; • import javax.awt.*; • import javax.awt.event.*; • public static void main(String[] args){ • MyFrame myWindow = new MyFrame(); • myWindow.addWindowListener(new WindowAdapter(){ • public void windowClosing(WindowEvent e) • { • System.exit(0); • }//windowClosing • }); • }

  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); } } ///:~

More Related