调用repaint() 在重量级和轻量级组件区别
2007-12-21 20:19
447 查看
1.轻量级的组件继承的是JComponent的子类,因此它们包含paintComoponent()方法;
所以当调用repaint()方法是表示应该尽早绘制该组件,即调用的是paintComoponent()方法。 只有当组件不透明的是才需要清除GUI组件的背景。大多数的组件在默认时是透明的。(设置组件的是否透明是JComponent的setOpaque方法来设定的)。
举例如下:
import java.awt.*;
import javax.swing.*;
public class CustomPanel extends JPanel{
public final static int CIRCLE = 1,SQUARE = 2;
private int shape;
public void paintComponent(Graphics g){
super.paintComponent(g); // 清除绘制的其他图像
if(shape == CIRCLE){
g.fillOval(50,10,60,60);
}
else if(shape == SQUARE){
g.fillRect(50,10,60,60);
}
}
public void draw(int shapeToDraw){
shape = shapeToDraw;
repaint();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CustomPanelTest extends JFrame{
private JPanel buttonPanel;
private CustomPanel myPanel;
private JButton circleButton,squareButton;
public CustomPanelTest(){
super("CustomPanel Test");
myPanel = new CustomPanel();
myPanel.setBackground(Color.green);
squareButton = new JButton("Square");
squareButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
myPanel.draw(CustomPanel.CIRCLE);
}
}
);
circleButton = new JButton("Circle");
circleButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
myPanel.draw(CustomPanel.SQUARE);
}
});
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(circleButton);
buttonPanel.add(squareButton);
Container container = getContentPane();
container.add(myPanel,BorderLayout.CENTER);
container.add(buttonPanel,BorderLayout.SOUTH);
setSize(300,150);
setVisible(true);
}
public static void main(String args[]){
CustomPanelTest application = new CustomPanelTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2. 重量级的组件继承的是Component类,当调用repaint()方法时是先调用Component类的update方法(用于清除组件的背景),并且update方法会调用paint方法。
所以当调用repaint()方法是表示应该尽早绘制该组件,即调用的是paintComoponent()方法。 只有当组件不透明的是才需要清除GUI组件的背景。大多数的组件在默认时是透明的。(设置组件的是否透明是JComponent的setOpaque方法来设定的)。
举例如下:
import java.awt.*;
import javax.swing.*;
public class CustomPanel extends JPanel{
public final static int CIRCLE = 1,SQUARE = 2;
private int shape;
public void paintComponent(Graphics g){
super.paintComponent(g); // 清除绘制的其他图像
if(shape == CIRCLE){
g.fillOval(50,10,60,60);
}
else if(shape == SQUARE){
g.fillRect(50,10,60,60);
}
}
public void draw(int shapeToDraw){
shape = shapeToDraw;
repaint();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CustomPanelTest extends JFrame{
private JPanel buttonPanel;
private CustomPanel myPanel;
private JButton circleButton,squareButton;
public CustomPanelTest(){
super("CustomPanel Test");
myPanel = new CustomPanel();
myPanel.setBackground(Color.green);
squareButton = new JButton("Square");
squareButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
myPanel.draw(CustomPanel.CIRCLE);
}
}
);
circleButton = new JButton("Circle");
circleButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
myPanel.draw(CustomPanel.SQUARE);
}
});
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(circleButton);
buttonPanel.add(squareButton);
Container container = getContentPane();
container.add(myPanel,BorderLayout.CENTER);
container.add(buttonPanel,BorderLayout.SOUTH);
setSize(300,150);
setVisible(true);
}
public static void main(String args[]){
CustomPanelTest application = new CustomPanelTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2. 重量级的组件继承的是Component类,当调用repaint()方法时是先调用Component类的update方法(用于清除组件的背景),并且update方法会调用paint方法。
相关文章推荐
- 重量级(awt)和轻量级(swing)组件的repaint()
- 轻量级组件和重量级组件的区别
- java 轻量级组件和重量级组件的区别
- java组件之重量级与轻量级区别
- java 轻量级组件与重量级组件的区别
- 轻量级组件与重量级组件的区别
- 重量级组件和轻量级组建区别
- 双缓冲消除闪烁在重量级Frame和轻量级组件Jframe中的区别
- 重量级与轻量级的区别
- 偏向锁、轻量级锁、重量级锁的升级以及区别
- java Swing AWT 轻量级组建 和 重量级组件
- JAVA中的重量级组件和轻量级组件
- 轻量级框架与重量级框架的区别
- Java轻量级组件和重量级组件
- 轻量级线程和重量级线程的定义是什么,他们的区别是什么?如何区分?
- 关于轻量级与重量级的区别。
- 轻量级框架跟重量级框架有何区别?
- 偏向锁,轻量级锁与重量级锁的区别与膨胀
- 知识扩展——轻量级和重量级框架的区别
- Synchronized的原理及自旋锁,偏向锁,轻量级锁,重量级锁的区别