首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

初学者,JAVA画图无法显示有关问题

2012-03-01 
菜鸟,求助....JAVA画图无法显示问题直接上代码import javax.swing.JFrameimport javax.swing.JPanelimpo

菜鸟,求助....JAVA画图无法显示问题
直接上代码
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
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.setLocation(450,250);
frame.setSize(300,250);
frame.setVisible(true);
}
}
class RectPanel extends JPanel{
protected void panitCompoment(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(5, 5, getWidth()/2-10, getHeight()/2-10);
g.drawRoundRect(getWidth()/2+5,5, getWidth()/2-10, getHeight()/2-10,60,30);
g.setColor(Color.cyan);
g.fill3DRect(5, getHeight()/2+5, getWidth()/2-10, getHeight()/2-10, true);
g.fill3DRect(getWidth()/2+5, getHeight()/2+5, getWidth()/2-10, getHeight()/2-10, false);
g.dispose();  
}
}
各位高手,怎么在窗口中不能显示矩形啊?谢谢各位诚心的解答啊........

[解决办法]
JPanel重新的方法不对

Java code
import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Color;import java.awt.Graphics;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.setLocation(450,250);        frame.setSize(300,250);        frame.setVisible(true);    }}class RectPanel extends JPanel{    @Override    public void paint(Graphics g){        super.paint(g);        g.setColor(Color.red);        g.drawRect(5, 5, getWidth()/2-10, getHeight()/2-10);        g.drawRoundRect(getWidth()/2+5,    5, getWidth()/2-10, getHeight()/2-10,60,30);        g.setColor(Color.cyan);        g.fill3DRect(5, getHeight()/2+5, getWidth()/2-10, getHeight()/2-10, true);        g.fill3DRect(getWidth()/2+5, getHeight()/2+5, getWidth()/2-10, getHeight()/2-10, false);//        g.dispose();    }} 

热点排行