inserta.java

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

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

/* Uses a text field to enter data and a canvas
 * to display the chart.  This is an exploratory
 * program to develop a GUI for insertion sort.
 */

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

public class InsertA extends Applet implements ActionListener{
  public static int ITEM_SIZE = 10;
  public static int CHART_SIZE 	= 100;
  TextField number = new TextField(5);
  DrawOn canvas = new DrawOn();
  int [] item = new int[ITEM_SIZE];
  int count = 0;

  public void init() { 
    add(number); 
    canvas.setSize(CHART_SIZE,CHART_SIZE);
    canvas.setBackground(Color.pink);
    add(canvas);
    number.addActionListener(this);
  }
  public void actionPerformed(ActionEvent event) {
    item[count++] = new Integer(number.getText()).intValue();
    number.setText("");
    canvas.repaint();
  }   
  
  class DrawOn extends Canvas {
    public void paint(Graphics g) {
      if (count > 0){
        int width = CHART_SIZE/count;
        for (int i=0; i<count; i++)
          g.fillRect(i*width,CHART_SIZE-item[i],width,item[i]);
      }
    }
  }
} 
       

⌨️ 快捷键说明

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