当前位置: 代码迷 >> Eclipse >> 初学者,JAVA画图无法显示有关问题
  详细解决方案

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

热度:94   发布时间:2016-04-23 14:12:11.0
菜鸟,求助....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();    }}
  相关解决方案