Java by API/java.beans/Beans — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Beans: getInstanceOf(Object bean, Class targetType)

import java.beans.Beans;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public MainClass(String name) {
    super("Revived Beans!");
    try {
      Object bean = Beans.instantiate(getClass().getClassLoader(), name);
      if (Beans.isInstanceOf(bean, JComponent.class)) {
        JComponent comp = (JComponent) Beans.getInstanceOf(bean, JComponent.class);
        getContentPane().add("Center", comp);
      } else {
        System.out.println("Bean is not a Component...");
      }
    } catch (java.io.IOException e1) {
      System.out.println("Error loading the serialized object");
    } catch (ClassNotFoundException e2) {
      System.out.println("Can"t find the class that goes with the object");
    }
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





Beans: instantiate(ClassLoader cls, String beanName)

import java.beans.Beans;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public MainClass(String name) {
    super("Revived Beans!");
    try {
      Object bean = Beans.instantiate(getClass().getClassLoader(), name);
      if (Beans.isInstanceOf(bean, JComponent.class)) {
        JComponent comp = (JComponent) Beans.getInstanceOf(bean, JComponent.class);
        getContentPane().add("Center", comp);
      } else {
        System.out.println("Bean is not a Component...");
      }
    } catch (java.io.IOException e1) {
      System.out.println("Error loading the serialized object");
    } catch (ClassNotFoundException e2) {
      System.out.println("Can"t find the class that goes with the object");
    }
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





Beans: isInstanceOf(Object bean, Class < ? > targetType)

import java.beans.Beans;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public MainClass(String name) {
    super("Revived Beans!");
    try {
      Object bean = Beans.instantiate(getClass().getClassLoader(), name);
      if (Beans.isInstanceOf(bean, JComponent.class)) {
        JComponent comp = (JComponent) Beans.getInstanceOf(bean, JComponent.class);
        getContentPane().add("Center", comp);
      } else {
        System.out.println("Bean is not a Component...");
      }
    } catch (java.io.IOException e1) {
      System.out.println("Error loading the serialized object");
    } catch (ClassNotFoundException e2) {
      System.out.println("Can"t find the class that goes with the object");
    }
  }
  public static void main(String[] args) {
    JFrame frame = new MainClass("javax.swing.JButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}