修飾子與參數
public void setLocation(int x, int y)
public void setLocation(Point p)
setLocation() 沒有回傳值 (return value) ,有兩個參數 (paramenter) 版本,第一個提供兩個整數型態 (type) 的整數,分別是 x 座標及 y 座標,第二個提供 Point 型態的物件 (object) ,該物件就有座標的屬性。
舉例如下
import java.awt.*;
import java.awt.event.*;
public class setLocationDemo {
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);
Button button2 = new Button("b2");
button2.setSize(100, 25);
button2.setLocation(50, 100);
Button button3 = new Button("b3");
button3.setSize(100, 25);
button3.setLocation(310, 400);
Button button4 = new Button("b4");
button4.setSize(100, 25);
button4.setLocation(500, 50);
Button button5 = new Button("b5");
button5.setSize(100, 25);
button5.setLocation(2, 420);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.setVisible(true);
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:setLocationDemo.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);
Button button2 = new Button("b2");
button2.setSize(100, 25);
button2.setLocation(50, 100);
Button button3 = new Button("b3");
button3.setSize(100, 25);
button3.setLocation(310, 400);
Button button4 = new Button("b4");
button4.setSize(100, 25);
button4.setLocation(500, 50);
Button button5 = new Button("b5");
button5.setSize(100, 25);
button5.setLocation(2, 420);編譯後執行,結果如下

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