日期:2014-05-20  浏览次数:20710 次

为什么输出什么也没有
package Test_11;

import java.awt.*;
import javax.swing.*;

public class DrawRectangles extends JFrame {
public DrawRectangles(){
setTitle("DrawRectangles");
getContentPane().add(new RectPanel());
}


public static void main(String[] args) {
DrawRectangles frame = new DrawRectangles();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setVisible(true);
}

}

class RectPanel extends JPanel {
protected void paintCompoent(Graphics g){
super.paintComponent(g);

// Set new color
g.setColor(Color.red);

// Draw a rectangle
g.drawRect(5, 5, getWidth() / 2 - 10, getHeight() / 2 - 10);

// Draw a rectangle
g.drawRoundRect(getWidth() / 2 + 5, 5, getWidth() / 2 - 10,
getHeight() / 2 - 10, 60, 30);

// Change the color to cyan
g.setColor(Color.cyan);

// Draw a 3D retangle
g.fill3DRect(5, getHeight() / 2 + 5, getWidth() / 2 - 10,
getHeight() / 2 - 10, true);

// Draw a raised 3D retangle
g.fill3DRect(getWidth() / 2 + 5, getHeight() / 2 + 5,
getWidth() / 2 - 10, getHeight() / 2 - 10, false);
}
}

------解决方案--------------------
楼主差点让你气死了。。照着打代码要仔细啊
你在你的paintCompoent这个方法里面打一个输出语句看看,这个方法根本没有被调用。
这就说明你的代码打错了。
再仔细看一下,
你的是:paintCompoent
而实际应该:paintComponent
差了一个字符,意义就不一样了。