4 1 import javax swing import javax swing

  • Slides: 37
Download presentation

範例4 -1 import javax. swing. *; //引用套件 import javax. swing. event. *; import java.

範例4 -1 import javax. swing. *; //引用套件 import javax. swing. event. *; import java. awt. event. *; public class Label. EX extends JFrame { Label. EX() { JLabel lb. Right = new JLabel("靠右", JLabel. RIGHT); JLabel lb. Left = new JLabel("靠左", JLabel. LEFT); JLabel lb. Center = new JLabel("置中", JLabel. CENTER); JLabel lb. Leading. Top = new JLabel("靠前緣", JLabel. LEADING); lb. Leading. Top. set. Vertical. Alignment(Swing. Constants. TOP); //設定垂直方向靠上對齊 JLabel lb. Trailing. Bottom = new JLabel("靠後緣", JLabel. TRAILING); lb. Trailing. Bottom. set. Vertical. Alignment(Swing. Constants. BOTTOM); //設定垂直方向靠下對齊 Container cp = get. Content. Pane(); //取得內容面版 cp. set. Layout(new Grid. Layout(2, 3, 10)); //取得佈局管理員

cp. add(lb. Left); //將元件加入面版 cp. add(lb. Center); cp. add(lb. Right); cp. add(lb. Leading. Top);

cp. add(lb. Left); //將元件加入面版 cp. add(lb. Center); cp. add(lb. Right); cp. add(lb. Leading. Top); cp. add(lb. Trailing. Bottom); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //設定關閉視窗將預設結束程式 set. Size(300, 250); //設定視窗的大小為 300 x 250 set. Visible(true); //顯示視窗框架 } public static void main(String args[]) { new Label. EX(); //宣告視窗框架物件 } }

欲運用某字型時,可以建立描述這個字型的Font物件,以下為Font類別的建構子。 public Font(String name, int style, int size) 加背景顏色—黃 lb. Green. set. Opaque(true); lb.

欲運用某字型時,可以建立描述這個字型的Font物件,以下為Font類別的建構子。 public Font(String name, int style, int size) 加背景顏色—黃 lb. Green. set. Opaque(true); lb. Green. set. Background(Color. yellow);

範例4 -2 import javax. swing. *; //引用套件 import java. awt. *; public class Color.

範例4 -2 import javax. swing. *; //引用套件 import java. awt. *; public class Color. Font. EX extends JFrame { Color. Font. EX() { JLabel lb. Red = new JLabel("紅"); lb. Red. set. Foreground(Color. RED); //以Color類別的常數取得代表紅色的Color物件, //並設定為標籤的前景顏色 JLabel lb. Green = new JLabel("綠"); lb. Green. set. Foreground(new Color(0, 255, 0)); //以整數的RGB值宣告代表綠色的Color物件, //並設定為標籤的前景顏色 JLabel lb. Blue = new JLabel("藍"); lb. Blue. set. Foreground(new Color(0. 0 f, 1. 0 f)); //以浮點數的RGB值宣告代表藍色的Colord物件, //並設定為標籤的前景顏色 JLabel lb. Times. Bold 15 = new JLabel("Times New Roman"); lb. Times. Bold 15. set. Font( new Font("Times New Roman", Font. BOLD, 20)); //設定標籤內文字使用的字型

Container cp = get. Content. Pane(); //取得內容面版 cp. set. Layout(new Grid. Layout(2, 2, 10));

Container cp = get. Content. Pane(); //取得內容面版 cp. set. Layout(new Grid. Layout(2, 2, 10)); //取得佈局管理員 cp. add(lb. Red); //將元件加入面版 cp. add(lb. Green); cp. add(lb. Blue); cp. add(lb. Times. Bold 15); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //設定關閉視窗將預設結束程式 pack(); //設定以適當大小顯示 set. Visible(true); //顯示視窗框架 } public static void main(String args[]) { new Color. Font. EX(); //宣告視窗框架物件 } }

範例 4 -3 import javax. swing. *; //引用套件 import java. awt. *; public class

範例 4 -3 import javax. swing. *; //引用套件 import java. awt. *; public class Icon. EX extends JFrame{ Icon. EX(){ JLabel lb. Icon = new JLabel(new Bitc. Icon(80, 80)); //以標籤顯示圖示 JLabel lb. Image. Icon = new JLabel(new Image. Icon(". \Icon\Bitc. gif")); //以圖檔建立顯示於標籤的圖示 Container cp = get. Content. Pane(); //取得內容面版 cp. set. Layout(new Grid. Layout(1, 2, 10)); //取得佈局管理員 cp. add(lb. Icon); //將元件加入面版 cp. add(lb. Image. Icon); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //設定關閉視窗將預設結束程式 pack(); //以適當大小顯示視窗 set. Visible(true); //顯示視窗框架 } public static void main(String args[]) { new Icon. EX(); //宣告視窗框架物件 } }

class Bitc. Icon implements Icon { //宣告實作Icon介面的圖示類別 private int width, height; public Bitc. Icon(int

class Bitc. Icon implements Icon { //宣告實作Icon介面的圖示類別 private int width, height; public Bitc. Icon(int w, int h){ //建構子 width = w; height = h; } //繪製圖示 public void paint. Icon(Component c, Graphics g, int x, int y){ g. draw. Rect(5, 5, 70); //繪製矩形 g. draw. String("位元文化", 15, 45); //繪製文字 } public int get. Icon. Width(){ //取得圖示寬度 return width; } public int get. Icon. Height(){ //取得圖示高度 return height; } }

import javax. swing. *; //引用套件 import javax. swing. event. *; Ex 4 -4 import

import javax. swing. *; //引用套件 import javax. swing. event. *; Ex 4 -4 import java. awt. *; import java. awt. event. *; public class Label. Icon. EX extends JFrame{ Label. Icon. EX(){ JLabel lb. Bitc. Center = new JLabel("文字置於圖示中央", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants. CENTER); //同時顯示文字與圖示的標籤, 圖示置中 lb. Bitc. Center. set. Horizontal. Text. Position(Swing. Constants. CENTER); //設定文字的水平方向相對於圖示採置中對齊 JLabel lb. Bitc. Top = new JLabel("文字置於圖示中央、靠上", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants. CENTER); //同時顯示文字與圖示的標籤, 圖示置中 lb. Bitc. Top. set. Horizontal. Text. Position(Swing. Constants. CENTER); //設定文字的水平方向相對於圖示採置中對齊 lb. Bitc. Top. set. Vertical. Text. Position(Swing. Constants. TOP); //設定文字的垂直方向相對於圖示採置上對齊 JLabel lb. Bitc. Bottom = new JLabel("文字置於圖示中央、靠下", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants. CENTER); //同時顯示文字與圖示的標籤, 圖示置中 lb. Bitc. Bottom. set. Horizontal. Text. Position(Swing. Constants. CENTER); //設定文字的水平方向相對於圖示採置中對齊 lb. Bitc. Bottom. set. Vertical. Text. Position(Swing. Constants. BOTTOM); //設定文字的垂直方向相對於圖示採置下對齊 JLabel lb. Bitc. Left = new JLabel("文字置於圖示左方、靠下", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants. CENTER); //同時顯示文字與圖示的標籤, 圖示置中 lb. Bitc. Left. set. Horizontal. Text. Position(Swing. Constants. LEFT); //設定文字的水平方向相對於圖示採置左對齊 lb. Bitc. Left. set. Vertical. Text. Position(Swing. Constants. BOTTOM); //設定文字的垂直方向相對於圖示採置下對齊

JLabel lb. Bitc. Right = new JLabel("文字置於圖示右方、靠上", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants.

JLabel lb. Bitc. Right = new JLabel("文字置於圖示右方、靠上", new Image. Icon(". \Icon\Bitc. gif"), Swing. Constants. CENTER); //同時顯示文字與圖示的標籤, 圖示置中 lb. Bitc. Right. set. Horizontal. Text. Position(Swing. Constants. RIGHT); //設定文字的水平方向相對於圖示採置右對齊 lb. Bitc. Right. set. Vertical. Text. Position(Swing. Constants. TOP); //設定文字的水平方向相對於圖示採置上對齊 Container cp = get. Content. Pane(); //取得內容面版 cp. set. Layout(new Grid. Layout(2, 3, 10)); //取得佈局管理員 cp. add(lb. Bitc. Center); //將元件加入面版 cp. add(lb. Bitc. Top); cp. add(lb. Bitc. Bottom); cp. add(lb. Bitc. Left); cp. add(lb. Bitc. Right); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //設定關閉視窗將預設結束程式 pack(); //以適當大小顯示視窗 set. Visible(true); //顯示視窗框架 } public static void main(String args[]) { new Label. Icon. EX(); //宣告視窗框架物件 } }

Practice 4. 2 課本 10 -43 (習題 10. 6 第 17 題) 假設有一個 Car

Practice 4. 2 課本 10 -43 (習題 10. 6 第 17 題) 假設有一個 Car 類別,表示車子的資訊 class Car { protected String owner; protected String id; final void show() { System. out. println("車主姓名: " + owner); System. out. println("車牌號碼: "+id); } 請依課本完成繼承之相關題目。