import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Houst {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setBackground(Color.red);
}
});
}
}
class DrawFrame extends JFrame
{
public DrawFrame(){
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
setSize(width/2,height/2);
setTitle("DrawFrame");
Image img=kit.getImage("icon.jpg");
setIconImage(img);
Component component=new Component();
add(component);
}

}
class Component extends JComponent{
public void Draw(Graphics g){
Graphics2D g2=(Graphics2D)g;
 double locationX=100;
double locationY=100;
  double width2=100;
  double height2=100;
Rectangle2D rect=new Rectangle2D.Double(locationX,locationY,width2,height2);
g2.draw(rect);
}
}按照常理来说,应该输出的是
红底的窗口中有一个上边距和下边距都为100像素的正方形
为什么这段代码输出的只有一个空白的窗口甚至底子是灰色的?
在此先谢谢各位了

解决方案 »

  1.   

    关于红色背景色,楼主需要这样做
    frame.getContentPane().setBackground(Color.red);关于画矩形,楼主需要覆盖paint方法而不是Draw方法,如下:
    class Component extends JComponent{
    @Override
        public void paint(Graphics g){
    super.paint(g);
            Graphics2D g2=(Graphics2D)g;
             double locationX=100;
            double locationY=100;
              double width2=100;
              double height2=100;
            Rectangle2D rect=new Rectangle2D.Double(locationX,locationY,width2,height2);
            g2.draw(rect);        
        }
    }
      

  2.   


    import java.awt.*;
    import java.awt.Dimension;
    import java.awt.geom.*;
    import javax.swing.*;public class Houst {
        public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
    MyFrame frame = new MyFrame();
        }
    });
        }
    }class MyFrame extends JFrame {
        Component component = new Component();
        Image img = null;    public MyFrame() {
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int width = screenSize.width;
    int height = screenSize.height;
    setTitle("DrawFrame");
    img = kit.getImage("Sample.jpg");
    setIconImage(img);
    add(component); setPreferredSize(new Dimension(width / 2, height / 2));
    pack();
    setVisible(true);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }class Component extends JComponent {//首先JComponent是一个抽象类,是用来扩展的,一般操作的都是继承他的各种组件 
        public Component() {
    setBackground(Color.RED);
    setVisible(true);    }    public void paintComponent(Graphics g) {// 重写paintComponent方法, Graphics2D g2 = (Graphics2D) g;
    double locationX = 100;
    double locationY = 100;
    double width2 = 100;
    double height2 = 100;
    g.setColor(getBackground());//设置当前颜色为背景色
    g.fillRect(0, 0, getWidth(), getHeight());//填充 g.setColor(Color.GRAY);
    Rectangle2D rect = new Rectangle2D.Double(locationX, locationY, width2,
    height2);
    g2.draw(rect);
    setBackground(Color.RED);    }
    }
      

  3.   

    按我1楼所说,改后是这样:public class Houst {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run(){
                    DrawFrame frame=new DrawFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                    frame.getContentPane().setBackground(Color.red);
                }
            });
        }
    }
    class DrawFrame extends JFrame
    {
        public DrawFrame(){
            Toolkit kit=Toolkit.getDefaultToolkit();
            Dimension screenSize=kit.getScreenSize();
            int width=screenSize.width;
            int height=screenSize.height;
            setSize(width/2,height/2);
            setTitle("DrawFrame");
            Image img=kit.getImage("icon.jpg");
            setIconImage(img);
            Component component=new Component();
            add(component);
        }
        
    }
    class Component extends JComponent{
    @Override
        public void paint(Graphics g){
    super.paint(g);
            Graphics2D g2=(Graphics2D)g;
             double locationX=100;
            double locationY=100;
              double width2=100;
              double height2=100;
            Rectangle2D rect=new Rectangle2D.Double(locationX,locationY,width2,height2);
            g2.draw(rect);        
        }
    }