2013-04-16 70 views
0

當我嘗試在Eclipse上運行下面的代碼時,Java小程序顯示出來,並且只顯示了一個正方形。但它沒有顯示(rect2)矩形爲什麼我的java「GRect」類不能在eclipse上工作?

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

您需要向我們展示了多一點的代碼 - 包括工作位和麪板這些控件上 – Elemental

+0

這裏是整個代碼。唯一可行的是GRect1。 GRect2沒有出現在日食上。 – Shazza

回答

0

矩形正在繪製的原始窗口之外。如果你只是拖動小程序窗口使其變大,你會看到一個填滿紅色的矩形。

所以,我對你的解決方案是簡單設置窗口的初始大小:

setSize(800, 800); 

,參數您GLabel是錯誤的。這樣做:

GLabel Label = new GLabel ("Hello world", 100, 75) ; 
相關問題