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

📄 example14_3.java

📁 不错的教程 适合中高级人员的使用
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;
class MyCanvas extends Canvas 
{  int n=-1;
   MyCanvas()
   {  setSize(150,120);setBackground(Color.pink);
   }
   public void paint(Graphics g)
   {  g.setColor(Color.red);
      g.drawString("部分清除时我将消失",10,12);
      g.drawString("我们正在学习repaint方法",10,80);
   }
   public void setN(int n)
   {  this.n=n;
   }
   public void update(Graphics g) 
   {  int width=0, height=0;
      width=getSize().width;height=getSize().height;
      if(n==0)
        {  g.clearRect(0,0,width,height);
           //paint(g); //如果取消该注释,update的功能就与父类相同。
        }
      else if(n==1)
        {  g.clearRect(2,2,width,40);
        }
   }
}
public class Example14_3 extends java.applet.Applet implements ActionListener
{  Button b1,b2;MyCanvas canvas;
   public void init()
   {  canvas=new MyCanvas();
      b1=new Button("全部清除"); b1.addActionListener(this);
      b2=new Button("部分清除"); b2.addActionListener(this);
      add(b1); add(b2); add(canvas);
   } 
   public void actionPerformed(ActionEvent e)
   {  if(e.getSource()==b1)
       {  canvas.setN(0);canvas.repaint();
       }  
      if(e.getSource()==b2)
       {  canvas.setN(1);canvas.repaint();
       }  
   }
}

⌨️ 快捷键说明

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