修飾子與參數
public Point getLocation()
public Point getLocation(Point rv)
getLocation() 的回傳值 (return value) 為 Point 型態 (type) 的物件 (object) ,該物件儲存座標資訊,有兩個參數 (paramenter) 版本,第一個不需要參數,另二個則是提供回傳值所用的 Point 物件。
舉例如下
import java.awt.*;
import java.awt.event.*;
public class getLocationDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWTDemo");
frame.setSize(640, 480);
frame.addWindowListener(new AdapterDemo());
frame.setLayout(null);
Button button1 = new Button("b1");
button1.setSize(100, 25);
button1.setLocation(200, 200);
Point p = button1.getLocation();
System.out.println("x: " + p.x);
System.out.println("y: " + p.y);
frame.add(button1);
frame.setVisible(true);
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:getLocationDemo.java
功能:示範 Java 程式
作者:張凱慶
時間:西元 2012 年 3 月 */此例先利用 Component 的 setLayout() 方法,將視窗版面配置設定為 null ,意即不具有任何版面配置
frame.setLayout(null);
然後建立按鈕後就以 Component 的 setSize() 與 setLocation() 方法,設定按鈕的大小與位置座標
Button button1 = new Button("b1");
button1.setSize(100, 25);
button1.setLocation(200, 200);最後以 Component 的 getLocation() 方法取得按鈕的座標,並且印在命令列視窗中
Point p = button1.getLocation();
System.out.println("x: " + p.x);
System.out.println("y: " + p.y);編譯後執行,結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 方法 | method |
| 回傳值 | return value |
| 型態 | type |
| 物件 | object |
| 參數 | parameter |
您可以繼續參考
AWT 元件
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html
沒有留言:
張貼留言