barton.java

来自「jbshortcourse」· Java 代码 · 共 63 行

JAVA
63
字号
import java.io.*; 
import java.net.*; 
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;

public class Barton extends Frame {
  Image im;    
  Font f;
  String fontDefault = "Serif-bolditalic-24";
  String msg;
  String msgDefault = "Donate Blood";
  Color savedColor;
  public Barton () {
    super ("Donate Blood");
    // Find Resource File
    InputStream is = getClass().getResourceAsStream(
                       "Barton.resources");
    // Load file and setup display resources
    try {
      Properties p = new Properties();
      p.load (is);
      f = Font.decode(
         p.getProperty("message.font", 
                       fontDefault));
      msg = p.getProperty ("message.text", msgDefault);
      // Get Image file
      String imageFile = p.getProperty ("image.file");
      URL url = getClass().getResource (imageFile);
      im = getToolkit().getImage (url);
    } catch (Exception e) {
      f = Font.decode (fontDefault);
      msg = msgDefault;
      im = null;
      System.err.println ("Using default properties.");
    }
    setFont (f);
    // Setup dealing with Window Closing
    enableEvents (AWTEvent.WINDOW_EVENT_MASK);
  }
  public void paint (Graphics g) {
    if (im == null) {
      savedColor = g.getColor();
      g.setColor (Color.red);
      g.fillRect (50, 25, 30, 80);
      g.fillRect (25, 50, 80, 30);
      g.setColor (savedColor);
    } else {
      g.drawImage (im, 25, 20, this);
    }
    g.drawString (msg, 50, 130);
  }
  protected void processWindowEvent (WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit (0);
    }
  }
  public static void main (String args[]) {
    Barton b = new Barton();
    b.setSize (300, 200);
    b.show();
  }
}

⌨️ 快捷键说明

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