繁体   English   中英

为什么我的Java“ GRect”类在eclipse上不起作用?

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

当我尝试在Eclipse上测试以下代码时,将显示Java小程序,并且仅显示一个正方形。 但是它没有显示(rect2)矩形

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

或(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) ;

整个代码:

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) ; 
    }     
}

该矩形是在原始窗口之外绘制的。 如果您只是拖动小程序窗口使其更大,则会看到一个红色实心矩形。

因此,我为您提供的解决方案是简单地设置窗口的初始大小:

setSize(800, 800);

GLabel的参数错误。 这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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