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

📄 borderpanel.java

📁 Java 入门书的源码
💻 JAVA
字号:
/* Revises Example 8.8 to put the button, 
 * the choice box, and the two checkboxes in 
 * the north region, and the canvas in the
 * center.
 */

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

public class BorderPanel extends Applet {
  Button draw = new Button("Draw");
  DrawOn canvas = new DrawOn();
  Choice color = new Choice();
  CheckboxGroup shapes = new CheckboxGroup();
  Checkbox square = new Checkbox("Square",false,shapes);
  Checkbox circle = new Checkbox("Circle",true,shapes);
  String [] colorName = {"Red","Green","Blue"};
  Color [] theColor = {Color.red,Color.green,Color.blue};

  public void init() {
    setLayout(new BorderLayout());
    Panel bar = new Panel();
    bar.add(draw);
    bar.add(color);
    bar.add(square);
    bar.add(circle);
    add(bar,"North");
    add(canvas,"Center");
    for (int i=0; i<colorName.length; i++) 
      color.add(colorName[i]);
    color.select(0);
    canvas.setBackground(Color.pink);
    draw.addActionListener(canvas);
  }

  class DrawOn extends Canvas implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      repaint();
    }
    public void paint(Graphics g) {
      g.setColor(theColor[color.getSelectedIndex()]);
      g.clearRect(20,20,100,100); 
      if (circle.getState())
         g.fillOval(20,20,100,100);
      else
         g.fillRect(20,20,100,100);
    }   
  }
}

⌨️ 快捷键说明

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