博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中paint方法和paintComponent方法的不同
阅读量:6881 次
发布时间:2019-06-27

本文共 2574 字,大约阅读时间需要 8 分钟。

/*   1.由Component.java源代码中可以看见其中的paint()方法体是空的,在Container中重写了该方法,其子类Window等也重写了该方法   2.由JComponent.java源代码中可以看见其中的paint()方法中调用paintComponent, paintChildren, paintBorder等方法;     所以该类中的paint方法会影响子组件的绘制, 而paintComponent方法只会影响该组件本身    3.paint方法 和 paintComponent方法都是对一个组件进行渲染的(组件创建并显示之后),并不是产生和显示该组件的!      4.swing 组件和 awt组件 paint方法的不同:awt组件的paint方法有自动刷新背景颜色的功能, 而swing组件一定要调用super.paint()*/import java.awt.*;import java.awt.event.*;import javax.swing.*;public class BkGnd extends Frame{   public BkGnd(){       setTitle("更换背景!");       setSize(new Dimension(600, 600));       setLayout(new FlowLayout());       myPanelOne panel = new myPanelOne();       panel.setBackground(Color.yellow);       panel.setPreferredSize(new Dimension(500, 500));       myPanelTwo pl = new myPanelTwo();       pl.setPreferredSize(new Dimension(400, 400));       pl.setBackground(Color.blue);       panel.add(pl);       add(panel);       addWindowListener(new MyClosingListener());   }   public static void main(String args[]){      BkGnd myFrame = new BkGnd();      myFrame.setVisible(true);   }} /*//第一种情况: 如果没有super.paint(g);则该面板上的组件将不能正常显示//如果加上super.paint(g);那么首先通过该方法将该面板上的组件显示出来,然后在执行super.paint(g);下面的绘图语句。会导致将该面板上的组件覆盖//父类的paint方法来负责调用paintComponent, paintChildren, paintBorder, update方法class myPanelOne extends JPanel{   public void paint(Graphics g){      super.paint(g);      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 400, 400, this);   }}  class myPanelTwo extends JPanel{   public void paintComponent(Graphics g){      super.paintComponent(g);      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 200, 200, this);   }} */ /*//第二种就可以的到正常预想的结果class myPanelOne extends JPanel{   public void paintComponent(Graphics g){      super.paintComponent(g);//用于绘制自身面板的背景      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 400, 400, this);   }} class myPanelTwo extends JPanel{   public void paintComponent(Graphics g){      super.paintComponent(g);      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 200, 200, this);   }} */  //重写Component类中的paint()方法的子类,super.paint(g) 不会影响其子组件。//第三种的效果等同于第二种的效果(正常的预想的结果)class myPanelOne extends Panel{   public void paint(Graphics g){      //super.paint(g);//加和不加都一样      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 400, 400, this);   }} class myPanelTwo extends Panel{   public void paint(Graphics g){      g.drawImage(new ImageIcon("zjy1.jpg").getImage(), 0, 0, 200, 200, this);   }} /**/ class MyClosingListener extends WindowAdapter{   public void windowClosing(WindowEvent e){      System.exit(0);   }}

转载地址:http://iubbl.baihongyu.com/

你可能感兴趣的文章
HTML5 入门基础
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>
C++文件操作(fstream)
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>
Python实践之(七)逻辑回归(Logistic Regression)
查看>>
PAT (Advanced Level) 1107. Social Clusters (30)
查看>>
使用Java程序调用MatLab
查看>>
什么是C++虚函数、虚函数的作用和使用方法
查看>>
Atitit.cto 与技术总监的区别
查看>>
关于【自证清白】
查看>>
手把手教你crontab排障
查看>>
订单编号
查看>>
纪念我曾经的 JAVA 姿势--转
查看>>
js 如何清除setinterval
查看>>
我为NET狂官方面试题-数据库篇答案
查看>>
聊聊eureka的delta配置
查看>>
Masonry 源码解读(下)
查看>>
Swift如何给应用添加3D Touch菜单
查看>>