简体   繁体   中英

Why doesn't my java “GRect” class work on eclipse?

When I try to test run the below code on Eclipse, the Java applet shows up, and only shows me a square. But it doesn't show the (rect2) rectangle

GRect Rect2 = new GRect (300, 75, 200, 100) ;
Rect2.setFilled (true) ; 
Rect2.setColor (Color.RED) ; 
add (Rect2) ; 

or the (GLabel) "hello world".

   GLabel Label = new GLabel ("Hello, world, 100, 75") ;     
   Label.setFont(new Font("Courier New", Font.ITALIC, 12));    
   Label.setColor (Color.RED);    add (Label) ;

The whole code:

import acm.graphics.*; 
import acm.program.* ; 
import java.awt.* ; 

public class Test extends GraphicsProgram  {
    private static final long serialVersionUID = 3365078119967111934L;

    public void run () { 
        GLabel Label = new GLabel ("Hello, world, 100, 75") ; 
        Label.setFont(new Font("Courier New", Font.ITALIC, 12));
        Label.setColor (Color.RED);
        add (Label) ; 

        GRect Rect1 = new GRect (10, 10, 50, 50) ;
        add(Rect1) ; 

        GRect Rect2 = new GRect (300, 75, 200, 100) ;
        Rect2.setFilled (true) ; 
        Rect2.setColor (Color.RED) ; 
        add (Rect2) ; 
    }     
}

The rectangle is being drawn outside of the original window. If you just drag the applet window to make it bigger, you will see a filled in red rectangle.

So my solution for you is to simply set the initial size of the window:

setSize(800, 800);

And the parameters to your GLabel are wrong. Do like this:

GLabel Label = new GLabel ("Hello world", 100, 75) ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM