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

绘图绘不出来,怎么回事?
我根据视频教学来写的代码,可是就是绘图画的圆显示不出来,怎么回事?其他一切正常。
代码如下:
g.fillOval(x, y, 30, 30);

------解决方案--------------------
这句话应该写在JComponent的paintComponent方法里面
Java code

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

public class TestComponent extends JComponent
{
    public TestComponent()
    {    
    }
    
    public void paintComponent(Graphics g)
    {
        int x = 0;
        int y = 0;
        g.fillOval(x, y, 30, 30);
    }
    
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add(new TestComponent());
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

------解决方案--------------------
这位哥们,你给的信息太少 了,怎么给你找原因啊?“其他一切正常。”可能吗?


------解决方案--------------------
把代码贴出来看看吧~
------解决方案--------------------
这是继承了Frame或者实现了相关的接口,需要重写paint方法,最好的方式去copy,用eclipse也有相关提示的,举例:
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new PaintFrame().launch();
}
}

class PaintFrame extends Frame{
public void launch(){
setBounds(200,200,640,480);//设置窗口的位置和大小
setVisible(true);
}
public void Paint(Graphics g){ //需要重写paint方法,不能错!
Color c = g.getColor();
g.setColor(Color.blue);
g.fillOval(50, 55, 30, 40);
g.setColor(Color.RED);
g.fillRect(80, 80, 40, 50);
g.setColor(c);

}
}