frameadapter.java

来自「Java 入门书的源码」· Java 代码 · 共 48 行

JAVA
48
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/*  Defines a subclass of WindowAdapter, overriding
 *  the windowClosing method but inheriting the default
 *  implementations of the other six window handling
 *  methods. Defines a subclass of Frame, adding a text
 *  field, and displaying the string the user enters.
 */
  
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class FrameAdapter extends Applet {
  private MyFrame f;

  public void init() {
    f = new MyFrame("MyFrame");
    f.setSize(150,150);
    f.show();
    f.addWindowListener(new CloseWindow());
  }
  class CloseWindow extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
      f.setVisible(false); 
      f.dispose();
    }
  }	
}

class MyFrame extends Frame implements ActionListener {
  Font font = new Font("Serif",Font.BOLD,24);
  TextField text = new TextField(10);
  public MyFrame(String title) {
    super(title);
    add(text,"North");
    text.addActionListener(this);
  }
  public void actionPerformed(ActionEvent event) {
    repaint();
  }
  public void paint(Graphics g) {
    g.setFont(font);
    g.drawString(text.getText(),20,100);
  }
}
    

⌨️ 快捷键说明

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