Flow Layout set Layoutnew Flow Layout set Layoutnew

  • Slides: 41
Download presentation

Flow. Layout デフォルトの設定 set. Layout(new Flow. Layout()); は set. Layout(new Flow. Layout(Flow. Layout. Center,

Flow. Layout デフォルトの設定 set. Layout(new Flow. Layout()); は set. Layout(new Flow. Layout(Flow. Layout. Center, 5, 5) と同様。

Flow. Layout 例 import java. awt. *; import java. applet. *; /* <APPLET CODE="Flow.

Flow. Layout 例 import java. awt. *; import java. applet. *; /* <APPLET CODE="Flow. Lay 1. class" WIDTH=400 HEIGHT=120> </APPLET> */ public class Flow. Lay 1 extends Applet{ public void init(){ set. Layout(new Flow. Layout()); //デフォルトは、Flow. Layout(Flow. Layout. Center, 5, 5)と同 様

Button btn 1 = new Button("ボタン 1"); add(btn 1); Button btn 2 = new

Button btn 1 = new Button("ボタン 1"); add(btn 1); Button btn 2 = new Button("ボタン 2"); add(btn 2); Button btn 3 = new Button("ボタン 3"); add(btn 3); } public void paint(Graphics g){ g. set. Color(Color. blue); g. set. Font(new Font("Serif", Font. BOLD + Font. ITALIC, 26)); g. draw. String("Flow. Layoutの ", 50, 100); //←x, y g. set. Color(Color. red); g. draw. String(" テスト!", 220, 100); } }

Flow. Layout 実行結果

Flow. Layout 実行結果

Boader. Layout コンポーネントを中央を中心にして、東西南北に配置。 North West Center East South 書式: Boader. Layout(int hgaps, int vgaps)

Boader. Layout コンポーネントを中央を中心にして、東西南北に配置。 North West Center East South 書式: Boader. Layout(int hgaps, int vgaps) hgaps:水平方向の間隔 vgaps:垂直方向の間隔 指定の方法: set. Layout(new Boader. Layout(5, 5)); Button bt 1 = new Button(“button-North”); add(bt 1, “North” ); (または add(bt 1, Boader. Layout. NORTH ); )

Boader. Layout 配置の位置 北 設定名 中央 南 東 西 “Center”または、Border. Layout. CENTER “South” または、Border.

Boader. Layout 配置の位置 北 設定名 中央 南 東 西 “Center”または、Border. Layout. CENTER “South” または、Border. Layout. SOUTH “East” または、Border. Layout. EAST “West” または、Border. Layout. WEST “North” または、Border. Layout. NORTH

Boader. Layout 例 import java. awt. *; import java. applet. *; /* <APPLET CODE="Border.

Boader. Layout 例 import java. awt. *; import java. applet. *; /* <APPLET CODE="Border. Lay 1. class" WIDTH=400 HEIGHT=220> </APPLET> */ public class Border. Lay 1 extends Applet { public void init(){ set. Layout(new Border. Layout()); //デフォルトは、Border. Layout(5, 5)と同様t

Button btn 1 = new Button("ボタン 1"); add(btn 1, "North"); Button btn 2 =

Button btn 1 = new Button("ボタン 1"); add(btn 1, "North"); Button btn 2 = new Button("ボタン 2"); add("South", btn 2); Button btn 3 = new Button("ボタン 3"); } public void paint(Graphics g) { g. set. Color(Color. blue); g. set. Font(new Font("Serif", Font. BOLD + Font. ITALIC, 26)); g. draw. String("Border. Layout の ", 50, 100); //←x, y g. set. Color(Color. red); g. draw. String(" テスト!", 220, 100); } }

Boader. Layout 実行結果

Boader. Layout 実行結果

Grid. Layout import java. awt. *; import java. applet. *; /* <APPLET CODE="Grid. Lay

Grid. Layout import java. awt. *; import java. applet. *; /* <APPLET CODE="Grid. Lay 1. class" WIDTH=400 HEIGHT=220> </APPLET> */ public class Grid. Lay 1 extends Applet { public void init() { set. Layout(new Grid. Layout(2, 3));

Button btn; for (int i = 1; i <= 9; i++){ add(btn = new

Button btn; for (int i = 1; i <= 9; i++){ add(btn = new Button("ボタン" + Integer. to. String(i))); } } public void paint(Graphics g){ g. set. Color(Color. blue); g. set. Font(new Font("Serif", Font. BOLD + Font. ITALIC, 26)); g. draw. String("Grid. Layoutの ", 50, 270); g. set. Color(Color. red); g. draw. String(" テスト!", 220, 270); } }

Grid. Layout 実行結果

Grid. Layout 実行結果

null. Layout(絶対位置の指定) import java. awt. *; import java. applet. *; /* <APPLET CODE="set. Bond

null. Layout(絶対位置の指定) import java. awt. *; import java. applet. *; /* <APPLET CODE="set. Bond 1. class" WIDTH=400 HEIGHT=300> </APPLET> */ public class set. Bond 1 extends Applet{ public void init(){ set. Layout(null); Button btn 1 = new Button("ボタン 1"); btn 1. set. Bounds(100, 60, 50); add(btn 1);

Button btn 2 = new Button("ボタン 2"); btn 2. set. Bounds(100, 150, 60, 50);

Button btn 2 = new Button("ボタン 2"); btn 2. set. Bounds(100, 150, 60, 50); add(btn 2); Button btn 3 = new Button("ボタン 3"); btn 3. set. Bounds(100, 200, 60, 50); add(btn 3); } public void paint(Graphics g){ g. set. Color(Color. blue); g. set. Font(new Font("Serif", Font. BOLD + Font. ITALIC, 26)); g. draw. String("set. Bounds の ", 50, 60); g. set. Color(Color. red); g. draw. String(" テスト!", 220, 60); } }

例. 描画 import java. awt. *; import java. awt. event. *; class My. Frame

例. 描画 import java. awt. *; import java. awt. event. *; class My. Frame extends Frame{ public My. Frame(String title){ set. Title(title); add. Window. Listener(new Window. Adapter(){ public void window. Closing(Window. Event e){ System. exit(0); } }); } public void paint(Graphics g){ String ss = "ABCabc"; int aa = 20, height=30, width=140;

g. draw. String(ss, aa, 50); g. set. Font(new Font("Serif", Font. BOLD, 24)); g. draw.

g. draw. String(ss, aa, 50); g. set. Font(new Font("Serif", Font. BOLD, 24)); g. draw. String(ss, aa, 80); g. draw. Rect(aa, 90, width, height); g. fill. Rect(aa, 130, width, height); } } public class Frm. Samp 1{ public static void main(String args[]){ My. Frame frm = new My. Frame("描画"); frm. set. Size(230, 180); frm. set. Visible(true); } }

例2.マウス import java. awt. *; import java. awt. event. *; class My. Frame extends

例2.マウス import java. awt. *; import java. awt. event. *; class My. Frame extends Frame implements Mouse. Listener{ int x 1=-1, y 1, x 2 = 999, y 2; public My. Frame(String title){ set. Title(title); add. Window. Listener(new Window. Adapter(){ public void window. Closing(Window. Event e){ System. exit(0); } }); add. Mouse. Listener(this); }

public void paint(Graphics g){ if (x 1 != -1){ if(x 2 != -1){ g.

public void paint(Graphics g){ if (x 1 != -1){ if(x 2 != -1){ g. draw. Line(x 1, y 1, x 2, y 2); } else { g. fill. Rect(x 1, y 1, 1, 1); } } } public void mouse. Entered(Mouse. Event me){} public void mouse. Exited(Mouse. Event me){}

public void mouse. Clicked(Mouse. Event me){ if (x 2 != -1){ x 1 =

public void mouse. Clicked(Mouse. Event me){ if (x 2 != -1){ x 1 = me. get. X(); y 1 = me. get. Y(); x 2 = -1; }else { x 2 = me. get. X(); y 2 = me. get. Y(); } repaint(); } public void mouse. Pressed(Mouse. Event me){} public void mouse. Released(Mouse. Event me){} }

public class Frm. Samp 2{ public static void main(String args[]){ My. Frame frm =

public class Frm. Samp 2{ public static void main(String args[]){ My. Frame frm = new My. Frame("マウス"); frm. set. Size(300, 200); frm. set. Visible(true); } } 実行結果

例3.コンポーネント利用 import java. awt. *; import java. awt. event. *; class My. Frame extends

例3.コンポーネント利用 import java. awt. *; import java. awt. event. *; class My. Frame extends Frame implements Action. Listener{ Button btn; Text. Field txt; public My. Frame(String title){ set. Title(title); set. Layout(null); add. Window. Listener(new Window. Adapter(){ public void window. Closing(Window. Event e){ System. exit(0); } }); btn= new Button("表示"); btn. add. Action. Listener(this); add(btn);

btn. set. Bounds(60, 70, 100, 25); txt=new Text. Field(""); add(txt); txt. set. Bounds(40, 140,

btn. set. Bounds(60, 70, 100, 25); txt=new Text. Field(""); add(txt); txt. set. Bounds(40, 140, 20); } public void action. Performed(Action. Event e){ txt. set. Text("ボタン押下"); } } public class Frm. Samp 3{ public static void main(String args[]){ My. Frame frm = new My. Frame("コンポーネント"); frm. set. Size(220, 120); frm. set. Visible(true); }