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

📄 buttoncanvaspress.java

📁 Java 入门书的源码
💻 JAVA
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Adds two buttons and a canvas to an applet.
 * The canvas registers with each button to 
 * listen for button presses.  Its actionPerfomed
 * saves the button label, and asks the operating
 * system to redraw the canvas (calling paint).
 * Paint displays a message if Print was pressed
 * but clears the canvas if Clear was pressed./
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class ButtonCanvasPress extends Applet {
  private Button print = new Button("Print");
  private Button clear = new Button("Clear");
  private DrawOn canvas = new DrawOn();

  public void init() {
    add(print); 
    add(clear);
    print.addActionListener(canvas);
    clear.addActionListener(canvas);
    add(canvas);
    canvas.setBackground(Color.pink);
    canvas.setSize(150,150);
  }
}
class DrawOn extends Canvas implements ActionListener {
  String command = "";

  public void actionPerformed(ActionEvent event) {
    command = event.getActionCommand();
    repaint();
  } 
  public void paint(Graphics g) {
    if (command.equals("Print")){ 
      g.drawString("Hi there",20,20);
      g.drawString("You just pressed",20,40);
      g.drawString("the print button.",20,60);
    }
  }
}
  

⌨️ 快捷键说明

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