150 likes | 280 Views
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
E N D
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
Swing Components Frame Combo boxButton List Menu Dialog Text Fields
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.
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)
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); • } • }
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); • }
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
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 • }); • }
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);
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); } } ///:~