JAVA - שיעור #9


1. Java 1.0 - AWT (Abstract Window Toolkit)
          Event model = hierarchical model.
    Java 1.1 - JavaBeans: component model, visual programming
          Event model = delegation model.
    Java 1.2 - Java Foundation Classes (JFC), Swing.

2. Button offButton = new Button();
    Button onButton = new Button("Hello");
    onButton.setLabel("Press me");
    String lbAttach = onButton.getLabel()

3. Label headLb = new Label("Anything to write");
     headLb.setText("Something else");
    String currentLabelStr = headLb.getText();

4. Checkbox noviceUserType = new Checkbox("Novice");
                     expeUserType = new Checkbox("Experienced");
    boolean b = expeUserType.getState();

    CheckboxGroup safa = new CheckboxGroup();
    Checkbox french = new Checkbox("French",safa,true);        
    Checkbox german = new Checkbox("German",safa,false);
    Checkbox english = new Checkbox("English",safa,false);

5. Choice computerChoice = new Choice();
    computerChoice.addItem("VAX");
    computerChoice.addItem("PC");
    computerChoice.addItem("Mac");
    computerChoice.select("VAX");
    String currentChoice = computerChoice.paramString();

    List nameList = new List();
    List nameList = new List(5,true);
    nameList.addItem("Ploni");


6. TextField txfA = new TextField();
    TextField txfB = new TextField(24);
    TextField txfC = new TextField("Type here");
    TextField txfD = new TextField("Type here",20);
    String text = txfC.getText();

    TextArea ta = new TextArea(4,20);
    TextArea ta = new TextArea(" This is some text to start " +
          "with.  Do you like it? Yes or no.",4,20);

7. Scrollbar scbH = new Scrollbar(Scrollbar.HORIZONTAL);
    Scrollbar scbV = 
                new Scrollbar(Scrollbar.VERTICAL,20,5,0,80);
    int currValue = scbV.getValue();

8. Container:  Frame, Applet, Panel
    Frame fr = new Frame("My Frame");
    fr.setLayout(new BorderLayout);    
    LayoutManager:  Dialog א BorderLayout

import java.awt.*;
public class FrameDemo {
    public static void main(String args[]) {
    Button buttons[] new Button[6];
    Frame fr = new Frame("We love Java");
    fr.setLayout(new BorderLayout());
    for (int i=1; i<6; i++) {
      buttons[i] = new Button("Button " + i);
    fr.add("North",buttons[1]);
    fr.add("South",buttons[2]);
    fr.add("East",buttons[3]);
    fr.add("West",buttons[4]);
    fr.add("Center",buttons[5]);
    fr.resize(200,200);
    fr.pack();
    fr.show(); 
} }   
  

 fr.setLayout(new FlowLayout(FlowLayout.LEFT));
 fr.add(buttons[1]);
 fr.add(buttons[2]);
 fr.add(buttons[3]);
 fr.add(buttons[4]);        
 fr.add(buttons[5]);
 fr.pack();
 fr.show();
 fr.setLayout(new GridLayout(3,2));

9. fr.setLayout(null);
    Button b1 = new Button("Java!");
    fr.add(b1);
    b1.setBounds(10,10,20,20);

10. import java.awt.*;
    public class awtclass {
      Panel topPanel, botPanel;
      public static void main(String args[]) {
        awtclass thisclass = new awtclass();
   //         .הגדרת מסגרת ברמה עליונה                                  
        Frame fr = new Frame("AWT Example");
   //           קריאה לשיטה להצגת מרכיבי הממשק במופע של       
   //                   .(frame instance) המסגרת                              
        thisclass.doUI(fr);
   //  Pack and display the frame.
       fr.pack();  fr.show(); 
      }                
    public void doUI(Frame fr) {
      fr.setLayout(new BorderLayout());
     //      .יצירת הפנלים שיחזיקו את מרכיבי הממשק
      topPanel = new Panel();     botPanel = new Panel();
     //     .הוספת המרכיבים למסגרת (frame)
      fr.add("North",topPanel);  fr.add("South",botPanel);
          botPanel.add(new Button("Button"));   } 
    }

11. event    target   action()
    mouseDown(Event, int, int)
    mouseDrag  mouseEnter  mouseExit  mouseMove  mouseUp
   public boolean mouseDown(Event e, int x, int y) {
        showStatus("Mouse at position: " + x + y);
   }

import java.awt.*;
public class OldModelExample extends Frame {
    Button trigger;  TextField text;
    public class OldModelExample() {
        setLayout(new FlowLayout());
        trigger = new Button("Click me!");   add(trigger);
        text = new TextField(20);    add(text);
        pack();  resize(100,200);  show();
    }
    public boolean action(Event event, Object argument) {
        if (event.target == trigger) {
            String s = text.getText() + "click ";
            text.setText(s);   return true;  }
         return false;  }
  }

import java.awt.*;
import java.awt.event.*;
public class DelegationExample extends Frame {
    Button trigger;  TextField text;
    public class DelegationExample() {
         setLayout(new FlowLayout());
       trigger = new Button("Click me!");  add(trigger);
       text = new TextField(20);          add(text);
       ActionListener handler = new ActionHandler(text);
       trigger.addActionListener(handler);
       pack();  setSize(100,200);  setVisible(true);  }
   }
public void main(String args[]) {
    new DelegationExample();  }
class ActionHandler implements ActionListener {
    private TextField text;
    public ActionHandler(TextField text) {
      this.text = text;   }
    public void actionPerformed(ActionEvent event) {
      String s = text.getText() + "click ";
      text.setText(s);  }
   }

12.                                          :סוגי מאורעות 
ActionListener                      actionPerformed(ActionEvent)
MouseListener                       mousePressed(MouseEvent)
                                    mouseReleased(MouseEvent)
                                    mouseEntered(MouseEvent)
                                    mouseExited(MouseEvent)
MouseMotionListener                 mouseMoved(MouseEvent)
                                    mouseDragged(MouseEvent)
KeyListener                         keyPressed(KeyEvent)
                                    keyReleased(KeyEvent)
...ועוד                             keyTyped(KeyEvent)



13. adapters
import java.awt.*;
import java.awt.event.*;
public class ClosableFrame extends Frame {
    public ClosableFrame() {
      addWindowListener(new WindowClosingHandler()); }  }
class WindowClosingHandler implements WindowListener {
    public void windowClosing(WindowEvent e) {
     System.exit(0); }
    public void windowOpened(WindowEvent e) {  }
    public void windowClosed(WindowEvent e) {  }
    public void windowIconified(WindowEvent e) {  }
    public void windowDeiconified(WindowEvent e) {  }
    public void windowActivated(WindowEvent e) {  }
    public void windowDeactivated(WindowEvent e) {  }
}
class WindowClosingHandler extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
      System.exit(0);  }  }

14.  inner classes
import java.awt.*;
import java.awt.event.*;
public class DraggingExample extends Frame {
     int x, y, width, height;
     public DraggingExample() {
      setSize(500,400);
*    addMouseListener(new MouseHandler(this));
*    addMouseMotionListener(new MouseMotionHandler(this));
    }
    public void paint(Graphics g) {
      g.drawRect(x, y, width, height);
     //      ...קוד לטיפול במקרה שיש טעויות בערכים      
      }
     }
class MouseHandler extends MouseAdapter {
    DraggingExample hook;
    public MouseHandler(DraggingExample hook) {
      this.hook = hook; 
    }
    public void mousePressed(MouseEvent event) {
      hook.x = event.getX();
      hook.y = event.getY();
      hook.width = hook.height = 0;
      hook.repaint(); }
  }
class MouseMotionHandler extends MouseMotionAdapter {
    DraggingExample hook;
    public MouseMotionHandler(DraggingExample event) {
      this.hook = hook; 
    }
    public void mouseDragged(MouseEvent event) {
      hook.width = event.getX() - hook.x;
      hook.height = event.getY() - hook.y;
      hook.repaint();
    }  
 }  
import java.awt.*;
import java.awt.event.*;
public class DraggingExample extends Frame {
     int x, y, width, height;
    public DraggingExample() {
      setSize(500,400);
*    addMouseListener(new MouseHandler());
*    addMouseMotionListener(new MouseMotionHandler());
    }
    public void paint(Graphics g) {
      g.drawRect(x, y, width, height);
     //      ...קוד לטיפול במקרה שיש טעויות בערכים      
    }  
}
class MouseHandler extends MouseAdapter {
    public void mousePressed(MouseEvent event) {
      x=event.getX();
      y=event.getY();
      width = height = 0;
      repaint();  }
  }
class MouseMotionHandler extends MouseMotionAdapter {
    public void mouseDragged(MouseEvent event) {
      width = event.getX() - x;
      height = event.getY() - y;
      repaint();  }
  }   
}