2013-02-23 136 views
-5

我的Java有問題,我需要幫助。在Java中需要幫助

假定接口GUIComponent存在以下方法: - 打開和關閉:無參數,返回布爾值 - 移動並調整大小:接受兩個整數參數並返回void 定義一個實現GUIComponent的類Window接口,並具有以下成員: - 寬度,高度,xPos和yPos整型實例變量,其中xPos和yPos初始化爲0 - 接受兩個整型變量(寬度後跟高度)的構造函數,用於初始化寬度和高度實例變量 - open的一個實現:向System.out發送「Window opened」並返回true - 一個關閉的實現,它將「Window closed」發送到System.out,並返回true - 一個resize實現,用於修改寬度和高度變量以反映指定的大小 - 移動的實現修改xPos和yPos以反映新位置

這是我輸入的代碼。

public class Window implements GUIComponent{ 
    private int width; 
    private int height; 
    private int xPos = 0; 
    private int yPos = 0; 
    public Window(int width, int height){ 
     this.width = width; 
     this.height = height; 
    } 
    public boolean open(){ 
     System.out.println("Window opened"); 
     return true; 
    } 
    public boolean close(){ 
     System.out.println("Window closed"); 
     return true; 
    } 
    public void resize(int width, int height){ 
     this.width = x; 
     this.height = y; 
    } 
    public int move(int xPos, int yPos){ 
     xPos = 1; 
     yPos = 1; 
    } 
} 

而我收到錯誤,我不知道該怎麼辦。

感謝

+2

你會得到什麼錯誤? – EClaesson 2013-02-23 22:19:38

+0

錯誤:您可能想使用1以外的數字 – user2103344 2013-02-23 22:20:33

+4

閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)。一個要點:_如果你的程序拋出一個異常,你是否包含異常,同時包含消息和堆棧跟蹤?_ – jlordo 2013-02-23 22:21:08

回答

1

這裏有一個問題 - 看看這個方法

public int move(int xPos, int yPos) { 
    xPos = 1; 
    yPos = 1; 
} 

它沒有返回值。方法名稱move表明它是一種操作方法,應聲明爲void

0

除了@ Reimeus的建議下,幾件事情我看到:

變化

public void resize(int width, int height){ 
    this.width = x; // What is x??? 
    this.height = y; // What is y??? 
} 

public void resize(int width, int height){ 
    this.width = width; 
    this.height = height; 
} 

而且改變

public int move(int xPos, int yPos){ 
    xPos = 1; // You should change the class variable. use this 
    yPos = 1; // and I thing you want the value to be passed by the method 
} 

public void move(int xPos, int yPos){ 
    this.xPos = xPos; 
    this.yPos = yPos; 
}