⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 top40.java

📁 jbshortcourse
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;

public class Top40 extends Frame implements ActionListener {
  MenuItem open, load, save, custom, quit;
  Component theComp = null;

  public Top40 () {
    super ("Casey's Countdown");
    Menu file = new Menu ("File");
    open = file.add (new MenuItem ("Open Serialized Bean"));
    open.addActionListener (this);
    load = file.add (new MenuItem ("Load CentralPerk Class"));
    load.addActionListener (this);
    save = file.add (new MenuItem ("Save Bean"));
    save.addActionListener (this);
    save.setEnabled (false);
    file.addSeparator();
    custom = file.add (new MenuItem ("Customize Bean"));
    custom.addActionListener (this);
    custom.setEnabled (false);
    file.addSeparator();
    quit = file.add (new MenuItem ("Quit"));
    quit.addActionListener (this);
    MenuBar mb = new MenuBar();
    mb.add (file);
    setMenuBar (mb);
    enableEvents (AWTEvent.WINDOW_EVENT_MASK);
  }
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      // Notify others we are closing
      super.processWindowEvent(e);
      System.exit(0);
    } else {
      super.processWindowEvent(e);
    }
  }
  public static void main (String args[]) {
    Frame f = new Top40();
    f.setSize(300, 300);
    f.show();
  }
  public void actionPerformed (ActionEvent e) {
    Object o = e.getSource();
    if (o == open) {
      loadBean();
    } else if (o == load) {
      newBean();
    } else if (o == save) {
      saveBean();
    } else if (o == custom) {
      customize();
    } else if (o == quit) {
      System.exit (0);
    }
  }
  private void loadBean () {
    setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {
      File f = new File ("CentralPerk.ser");
      FileInputStream fis = new FileInputStream(f);
      ObjectInputStream ois = new ObjectInputStream (fis);
      Component c = (Component)ois.readObject();
      add (c, BorderLayout.CENTER);
      ois.close();
      fis.close();
      checkCustomize(c.getClass());
      theComp = c;
      save.setEnabled (true);
    } catch (Exception e) {
      System.out.println ("Unable to load: CentralPerk.ser");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void newBean () {
    try {
      setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      Component c = (Component)Beans.instantiate (null, "Marcel.CentralPerk");
      add (c, BorderLayout.CENTER);
      checkCustomize(c.getClass());
      theComp = c;
      save.setEnabled (true);
    } catch (Exception e) {
      System.out.println ("Unable to create Marcel.CentralPerk");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void saveBean () {
    setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {
      File f = new File ("CentralPerk.ser");
      FileOutputStream fos = new FileOutputStream(f);
      ObjectOutputStream oos = new ObjectOutputStream (fos);
      oos.writeObject (theComp);
      oos.close();
      fos.close();
    } catch (Exception e) {
      System.out.println ("Unable to write: CentralPerk.ser");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void customize () {
    try {
      BeanInfo bi = Introspector.getBeanInfo (theComp.getClass());
      BeanDescriptor bd = bi.getBeanDescriptor();
      Class c = bd.getCustomizerClass();
      Customizer customizer = (Customizer) c.newInstance();
      customizer.setObject (theComp);
      final Dialog d = new Dialog (this, "Customizer", true);
      d.add ((Component)customizer, BorderLayout.CENTER);
      Button b = new Button ("Done");
      d.add (b, BorderLayout.SOUTH);
      b.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent e) {
            d.dispose();
          }
        }
      );
      d.pack();
      d.show();
    } catch (Exception e) {
      System.out.println ("Oops");
    }
  }
  private void checkCustomize(Class c) {
    try {
      BeanInfo bi = Introspector.getBeanInfo (c);
      BeanDescriptor bd = bi.getBeanDescriptor();
      custom.setEnabled (bd.getCustomizerClass() != null);
    } catch (IntrospectionException e) {
      System.out.println ("Oops");
      custom.setEnabled (false);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -