JAVA - שיעור #12

1. class appThread {
    public static void main(String args[]) {
        new myThread("One").start();
        new myThread("Two").start();   }    
    }
    class myThread extends Thread {
        public myThread(String str) {
            super(str);   }
    public void run() {
        for (int i=0; i<10; i++) {
            System.out.println("Thread " + getName() +
                                 " : " + i);
             try {
                 sleep(100);
            } catch(InterruptedException e) {   }
        }
    }
}

2. import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    public class dualCode extends Applet {
           static class WL extends WindowAdapter {
             public void windowClosing(WindowEvent e) {
                System.exit(0); }
         }
         public static void main(String args[]) {
            Frame f = new Frame("Dual Applet/Application"):
            f.addWindowListener(new WL());
            dualCode dc = new dualCode();
            dc.init();
            f.add("Center",dc);
            f.setSize(100,100);
            f.setVisible(true);
        }
        public void init() {
            setLayout(new FlowLayout());
            add(new Button("Button"));
        }
    }

3. import sun.awt.image.URLImageSource;
    img = Toolkit.getDefaultToolkit();
        createImage(new URLImageSource(url));

    getImage()        getAppletContext()

4. import java.awt.*;
    import java.awt.event.*;  
    public class uiApp {
        static class WL extends WindowAdapter {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }
        public static void main(String args[]) {
            frameClass fc = new frameClass("UI Demo");
              fc.addWindowListener(new WL()); 
       }
    }
    class frameClass extends Frame {
        public frameClass(String str) {
            super(str);  addElems();
            setSize(100,200); setVisible(true);  }
        public void addElems() {
            setLayout(new FlowLayout());
            add(new Button("Button 1"));
            add(new Button("Button 2"));
            add(new Button("Button 3"));
            add(new Button("Button 4"));   }
    }

5.  
//  CutAnd Paste.java
//   Using the clipboard from Java 1.1
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class CutAndPaste extends Frame {
  MenuBar mb = new MenuBar();
  Menu edit = new Menu("Edit");
  MenuItem
    cut = new MenuItem("Cut");
    copy = new MenuItem("Copy");
    paste = new MenuItem("Paste");
   TextArea text = new TextArea(20,20);
   Clipboard clipbd = getToolkit().getSystemClipboard();
   public CutAndPaste() {
     cut.addActionListener(new CutL());
     copy.addActionListener(new CopyL());
     paste.addActionListener(new PasteL());
     edit.add(cut);
     edit.add(copy);
     edit.add(paste);
     mb.add(edit);
     setMenuBar(mb);
     add(text, BorderLayout.CENTER);  }

class CopyL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    String selection = text.getSelectedText();
    StringSelection clipString = new StringSelection(selection);
    clipbd.setContents(clipString, clipString);
  }
}

class CutL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    String selection = text.getSelectedText();
    StringSelection clipString = new StringSelection(selection);
    clipbd.setContents(clipString,clipString);
    text.replaceRange("",text.getSelectionStart(),
                                       text.getSelectionEnd());
  }
}

class PasteL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    Transferable clipData =
      Clipbd.getContents(CutAndPaste.this);
    try {
      String clipString = (String)clipData.getTransferData( 
       DataFlavor.stringFlavor);
      text.replaceRange(clipString,text.getSelectionStart(),
                   text.getSelectionEnd()); 
      }  catch (Exception ex) {
           System.out.println("not String flavor"); }
    }
  }

public static void main(String args[]) {
  CutAndPaste cp = new CutAndPaste();
  cp.setSize(300,200); 
  cp.setVisible(true);
  cp.addWindowListener (new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      System.exit(0);  }
    });
  }
}

6.
// BangBean.java:  a graphical Bean
package BangBean;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class BangBean extends Canvas implements Serializable {
  protected inm, ym;
  protected int cSize = 20;   // Circle size
  protected String text = "Bang!";
  protected int fontSize = 40;
  protected Color tColor = Color.red;
  protected ActionListener actionListener;
  public BangBean() {
    addMouseListener(new ML());
    addMouseMotionListener(new MM());
  }
public int getCircleSize() { return cSize); }
public void setCircleSize(int newSize) { cSize = newSize; }
public String getBangText() { return text; }
public void setBangText(String newText) {text = newText; }
public int getFontSize() { return fontSize; }
public void setFontSize(int newSize) {fontSize = newSize; }
public int getTextColor() { return tColor; }
public void setTextColor(Color newColor) { tColor = newColor; }
public void paint(Graphics g) {
  g.setcolor(Color.black);
  g.drawOval(xm-cSize/2, ym-cSize/2, cSize, cSize); 
}

public void addActionListener(ActionListener l)  
  throws TooManyListenersException {
    if (actionListener != null) 
      throw  new TooManyListenersException();
    actionListener = l;
}

public void removeActionListener (ActionListener l) {
  actionListener = null;  }

class ML extends MouseAdapter {
  public void mousePressed(MouseEvent e) {
    Graphics g = getGraphics();
    g.setColor(tColor);
    g.setFont(new Font("TimesRoman",Font.BOLD,fontSize));
    int width = g.getFontMetrics().stringWidth(text);
    g.drawString(text, (getSize()..Width-Width)/2),
                                   getSize().height/2);
    g.dispose();
   if (actionListener != null) 
      actionListener.actionPerformed (new ActionEvent(BangBean.this,
                ActionEvent.ACTION_PERFORMED, null);  }
  }

class MM extends MouseMotionAdapter {
  public void mouseMoved(MouseEvent e) {
    xm = e.getX();  ym = e.getY();
    repaint();  }
}

public Dimension getPreferredSize()  {
  return new Dimension(200,.200); }

public static void main(String args[]) {
  BangBean bb = new BangBean();
  try {
    bb.addActionListener(new BBL());
  } catch (TooManyListenersException e) { }
Frame aFrame = new Frame("BangBean Test.");
aFrame.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      System.exit(0); }
  }
  aFrame.add(bb,BorderLayout.CENTER);
  aFrame.setSize(300,300);
  aFrame.setVisible(true);
}

static class BBL implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    System.out.println("BangBean action");  }
  }
}


Manifest-Version:  1.0

Name: bangbean/BangBean.class
Java-Bean: True

Jar cfm BangBean.jar BangBean.mf bangbean

7. Connection dbConnection;
    Statement sqlStatement;
    ResultSet results; 
    Class.forName("Drivers.newDriver.JdbcOdbcCDR"); 
    try {    
          dbConnection  =  DriverManager.getConnection(
            "jdbc:odbc:employee.dta","D.Davis","Pli00&j0");
          sqlStatement = dbConnection.createStatement();
          results = sqlStatement.executeQuery(
            "SELECT Name, Address FROM EmpTable");
          while (results.next()) { ... }
      } catch (SQLException e) {
          System.out.println("SQL Error occurred");
      }
  }