Swing 2002 5 2 Java 2005 1 28 Swing 2005 3 4 2006 3 6 Copyright c 2002 2006 Daikoku Manabu



Similar documents
text_12.dvi

text_13.dvi

2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible

KeyListener init addkeylistener addactionlistener addkeylistener addkeylistener( this ); this.addkeylistener( this ); KeyListener public void keytyped

B 10 : N ip2003f10.tex B : 9/12/ :02 p.1/71

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec

Safari AppletViewer Web HTML Netscape Web Web 15-1 Applet Web Applet init Web paint Web start Web HTML stop destroy update init Web paint start Web up

Java言語 第1回

AWT setcolor, drawstring Java AWT Abstract Window Toolkit Graphics AWT import import java.awt.* ; // AWT Graphics import java.awt.graphics; // AWT Gra

AWT setcolor, drawstring Java AWT Abstract Window Toolkit Graphics AWT import import java.awt.* ; // AWT Graphics import java.awt.graphics; // AWT Gra

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

awt の主要なクラスを下記に示す クラス Component Container Button Label Panel Frame 説明画面にユーザインターフェイス要素として表示し, ユーザとのやり取りを行うコンポーネントを表すすべてのコンポーネントのスーパークラスになる ほかのコンポーネントを含

< F2D82518E9F8AD CC834F CC8CFC82AB82C68D4C>

Java 2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q 2.

Microsoft PowerPoint - swing2.ppt

Chapter JDK KeyListener keypressed(keyevent e ) keyreleased(keyevent e ) keytyped(keyevent e ) MouseListener mouseclicked(mouseeven

< F2D F B834E2E6A7464>

< F2D82518CC282CC D2E6A7464>

< F2D89BA8EE882C E6A7464>

Local variable x y i paint public class Sample extends Applet { public void paint( Graphics gc ) { int x, y;... int i=10 ; while ( i < 100 ) {... i +=

< F2D834F838C A815B A CC>

< F2D B838A835882CC8CF68EAE2E6A7464>

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

< F2D825282CC947B909482CC A815B83682E6A>

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

< F2D A839382CC906A2E6A7464>

< F2D E E6A7464>

public class MyFrame { private JPanel panel1; private JPanel panel2; private JPanel panel3; private JPanel panel4; private JLabel label1; private JTex

Applet java.lang.object java.awt.component java.awt.container java.awt.panel java.applet.applet

Prog1_12th

Java 2 - Lesson01

Chapter 20. [ ] ; [ ] = new [ ] ; Color colors [ ] = new Color[ 20 ]; // 20 Button operations [ ] = new Button[ 10 ]; // 10 colors[ 3 ] = new Color( 1

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

Java言語 第1回

r3.dvi

ソフトウェア基礎演習 課題

2

< F2D82B682E182F182AF82F12E6A7464>

public class Kadai _02 { public static void main(string[] args) { MyFrame frame = new MyFrame("Kadai _02"); (2) フレームのクラス名は MyFrame とし 以下

I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Jav

< F2D82518E9F8AD CC95BD8D7388DA93AE2E6A7464>

< F2D A838B838D96402E6A7464>

r14.dvi

Microsoft PowerPoint - prog12.ppt

Object MenuComponent MenuBar MenuItem Menu CheckboxMenuItem

Microsoft PowerPoint prog1_doc2x.pptx

アプレットの作成

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

< F2D B825082CC96E291E82E6A7464>

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

vol.30.}...`.X...b.h

I. java.awt.rectangle java.lang.math random Java TM API java.awt Rectangle Rectangle (x,y)... public int x Rectangle X public int y Rectangle Y public

r3.dvi

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi

Microsoft PowerPoint prog1_doc2.pptx

2 p.2 2 Java > javac Hello0.java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.j

< F2D92DE82E8914B82CC977088D32E6A7464>

r2.dvi

Java演習(4) -- 変数と型 --

Prog1_12th

6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent thread, 1 GUI 6.0.2, mutlithread C

r8.dvi

r4.dvi

GUIプログラムⅤ

< F2D8EA CE909482CC92EA82852E6A7464>

GUIプログラムⅣ

Microsoft Word - Java4.DOC

Java演習(9) -- クラスとメソッド --

ÿþ˜u#u·0¹0Æ0à0

目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i

次の演習課題(1),(2)のプログラムを完成させよ

ブロック崩し Step1 矢印キーで左右に動かせるパドルを描画する < パドルの表現方法 > パドルは java.awt パッケージの Rectangle という Java が用意しているクラスを使う これは四角形を表すクラスで 左上の点の座標と幅 高さをもっている (x, y) Rectangle

Thread

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

任意の加算プログラム

Java演習(6) -- 条件分岐 --

ガイダンス

try catch Exception Java try catch try { } catch ( Exception e ) { } e 16-1 try catch 0 try { int x = 0; int y = 10 / x; } catch ( Exception e ) { Sys

ガイダンス

r14.dvi

ガイダンス

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3

<4D F736F F F696E74202D AC C8899E D834F E >

r2.dvi

Cir

アプレットⅣ

/ ( ) 8/7/ :21 p.2/64

:30 12:00 I. I V II. III. IV. ( a d) V. VI : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeyliste

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

r6.dvi

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

Java演習(2) -- 簡単なプログラム --

問1

......() JButton

LMNtal LMNtal LMNtal JAVA JAVA JAVA LMNtal LMNtal LMNtal

アプレットⅢ

I. (i) Java? (A). Foo_Bar (B). G day (C). 999 (D). Golgo13 (ii)? (A). Java public (B). Java (C). Java JavaScript (D). Java C Java C (iii)? (A). Java (

Transcription:

Swing

Swing 2002 5 2 Java 2005 1 28 Swing 2005 3 4 2006 3 6 Copyright c 2002 2006 Daikoku Manabu

3 1 Swing 8 1.1 Swing.................................... 8 1.1.1.......................... 8 1.1.2 GUI............................. 8 1.1.3 AWT Swing................................ 8 1.1.4............................... 8 1.1.5........................... 9 1.2........................................ 9 1.2.1.................................. 9 1.2.2................................ 9 1.2.3.......................... 9 1.2.4................... 10 1.2.5............................... 10 1.2.6............................. 10 1.2.7............................. 11 1.2.8................. 11 1.3............................. 12 1.3.1...................... 12 1.3.2.......................... 12 1.3.3.......................... 14 2 14 2.1................................ 14 2.1.1..................................... 14 2.1.2..................................... 14 2.1.3.......................... 15 2.1.4....................... 15 2.1.5.................................. 16 2.2........................................... 16 2.2.1............................ 16 2.2.2 Color................. 17 2.2.3.......................... 17 2.3...................................... 18 2.3.1........................... 18 2.3.2...................................... 18 2.3.3..................................... 19 2.3.4..................................... 20 2.4..................................... 20 2.4.1.......................... 20 2.4.2................................ 21 2.5.................................... 22 2.5.1............................... 22 2.5.2........................ 22 3 23 3.1.................................... 23 3.1.1............................... 23 3.1.2..................... 23 3.1.3..................... 24 3.1.4............................ 24 3.2................................... 24 3.2.1............................... 24

4 3.2.2............................. 25 3.2.3........................... 26 3.2.4............................ 26 3.2.5................................. 27 3.3................................ 28 3.3.1....................... 28 3.3.2.................................. 29 3.3.3............................ 30 3.4................................ 31 3.4.1........................ 31 3.4.2.................................. 31 3.5........................................ 32 3.5.1................... 32 3.5.2................................ 32 3.5.3............................ 33 3.6......................................... 34 3.6.1............................ 34 3.6.2...................... 34 3.6.3.................................. 35 3.6.4........................... 35 4 39 4.1..................................... 39 4.1.1................................ 39 4.1.2................................. 39 4.1.3.................................... 39 4.1.4............................... 40 4.1.5........................... 40 4.1.6................................. 41 4.2...................................... 41 4.2.1................. 41 4.2.2............................. 42 4.2.3............................. 42 4.2.4..................................... 43 4.3..................................... 44 4.3.1............................ 44 4.3.2............................. 44 4.3.3............................. 44 4.3.4................................ 45 4.4........................................ 46 4.4.1................................. 46 4.4.2.................................... 47 4.4.3............................... 47 4.4.4................................. 47 4.4.5................................. 48 4.4.6........................... 49 4.4.7................................. 50 4.4.8............................... 50 4.5................................ 51 4.5.1....................... 51 4.5.2........................ 51

5 5 52 5.1......................................... 53 5.1.1............................... 53 5.1.2................................. 53 5.1.3................................ 53 5.2 HTML......................................... 54 5.2.1 HTML..................... 54 5.2.2................................ 54 5.3................................. 55 5.3.1.......................... 55 5.3.2........................ 56 5.4.................................... 57 5.4.1............................ 57 5.4.2............................... 57 5.4.3................................ 58 5.5................................... 60 5.5.1........................... 60 5.5.2............................ 60 5.5.3 URL......................... 61 5.5.4.......................... 61 6 63 6.1.................................... 63 6.1.1................................. 63 6.1.2................................ 63 6.2..................................... 64 6.2.1............................. 64 6.2.2.................................. 65 6.2.3............................... 65 6.3.................................... 66 6.3.1........................... 66 6.3.2....................... 67 6.3.3.......................... 67 6.3.4............................ 68 6.4........................................ 69 6.4.1................................ 69 6.4.2................................ 69 7 70 7.1.......................... 70 7.1.1........................ 70 7.1.2....................... 70 7.1.3............... 71 7.1.4...................... 72 7.2.......................... 73 7.2.1 JFileChooser............................ 73 7.2.2............................. 74 7.3.............................. 76 7.3.1............. 76 7.3.2........... 76 7.3.3........... 77 7.3.4.................. 79

6 8 80 8.1................................... 80 8.1.1................................ 80 8.1.2........................... 80 8.1.3.......................... 80 8.1.4............................. 80 8.2...................................... 81 8.2.1............................... 81 8.2.2............................ 81 8.2.3...................... 81 8.2.4.................................. 82 8.2.5.......................... 82 8.3................................. 83 8.3.1............................... 83 8.3.2................................. 83 8.3.3........................... 84 8.3.4................. 84 8.4...................................... 85 8.4.1............................. 85 8.4.2............................... 85 8.4.3................................. 85 9 86 9.1.................................... 86 9.1.1............................... 86 9.1.2................................ 86 9.2.................................... 87 9.2.1.................. 87 9.2.2........................ 87 9.3.................................... 88 9.3.1........................... 88 9.3.2............................ 89 9.3.3................................ 89 9.3.4................................ 89 9.4.............................. 90 9.4.1...................... 90 9.4.2...................... 91 9.5.................................... 92 9.5.1.................................... 92 9.5.2...................... 92 9.5.3........................... 92 9.5.4........................... 93 9.5.5................................ 93 9.5.6................................ 93 9.5.7............................ 94 9.6.................................... 95 9.6.1........................... 95 9.6.2........................... 95 9.6.3............................ 96

7 10 98 10.1...................................... 98 10.1.1............................... 98 10.1.2............................. 99 10.1.3 HTML................... 99 10.1.4............................ 99 10.1.5................. 99 10.1.6 HTML.............. 100 10.1.7................... 102 10.1.8....................... 102 10.1.9..................... 103 10.1.10............................... 104 10.2......................................... 105 10.2.1................................ 105 10.2.2................................ 106 10.2.3.......................... 106 10.2.4................................. 106 10.2.5................. 106 10.2.6.......................... 107 10.2.7...................................... 107 10.3.................................... 108 10.3.1........................... 108 10.3.2................................ 108 10.3.3................................ 109 10.3.4................................. 109 10.4...................................... 110 10.4.1............................. 110 10.4.2........................... 110 10.4.3............................ 110 10.4.4................................ 110 10.5................................ 111 10.5.1....................... 111 10.5.2 Swing....................... 112 10.5.3................. 112 10.5.4............ 112 10.6..................................... 113 10.6.1............................. 113 10.6.2..................... 114 10.6.3........................ 114 10.7........................................ 115 10.7.1................................ 115 10.7.2.................. 115 116 117

8 1 Swing 1 Swing 1.1 Swing 1.1.1 (user interface) CUI GUI 2 CUI(character user interface) CUI GUI(graphical user interface) GUI (pointing device) 1.1.2 GUI GUI GUI API(application programming interface) GUI API API GUI John Ousterhout Tk Tk toolkit 1.1.3 AWT Swing Java (package) Java GUI AWT Swing AWT(Abstract Window Toolkit) Java Swing Swing AWT AWT Swing GUI Swing AWT 1.1.4 Swing Swing GUI Swing GUI AWT Swing AWT Java Java Java Java Java

1.2. 9 1.1.5 Java Swing AWT AWT Swing java.awt javax.swing AWT java.awt.color GUI Swing AWT import Swing AWT Swing J JFrame JButton J AWT 1.2 1.2.1 (window) 2 (frame) (dialog box) GUI 1.2.2 Java GUI (component) Swing AWT 1.2.3 Swing JFrame JFrame frame = new JFrame(); frame

10 1 Swing JFrame.DO_NOTHING_ON_CLOSE JFrame.HIDE_ON_CLOSE JFrame.DISPOSE_ON_CLOSE JFrame.EXIT_ON_CLOSE 1.1: 1.2.4 GUI setdefaultcloseoperation 1.1 frame. frame 1.2.5 setsize 2 1 2 frame. frame 400 300 1.2.6 setvisible true false frame. frame Swing PFrame.java public class PFrame {

1.2. 11 JFrame frame = new JFrame(); frame. frame. frame. 1.2.7 (title bar) (title) JFrame 1 PTitle.java public class PTitle { JFrame frame = new JFrame(" frame. frame. frame. settitle frame.settitle(" frame 1.2.8 JFrame PSubclass.java public class PSubclass extends JFrame { PSubclass(String title) { PSubclass frame = new PSubclass("

12 1 Swing JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE JOptionPane.PLAIN_MESSAGE 1.2: 1.3 1.3.1 JOptionPane JOptionPane.showMessageDialog JOptionPane.showMessageDialog (1) null (2) (3) (4) 4 4 JOptionPane 1 null 2 3 4 1.2 PMessage.java public class PMessage extends JFrame { PMessage(String title) { PMessage frame = new PMessage( " JOptionPane.showMessageDialog(frame, " ", ", JOptionPane.INFORMATION_MESSAGE); 1.3.2

1.3. 13 JOptionPane.YES_NO_OPTION JOptionPane.YES_NO_CANCEL_OPTION JOptionPane.OK_CANCEL_OPTION 1.3: JOptionPane.YES_OPTION JOptionPane.NO_OPTION JOptionPane.OK_OPTION JOptionPane.CANCEL_OPTION 1.4: JOptionPane.showConfirmDialog JOptionPane.showConfirmDialog 4 3 4 4 1.3 JOptionPane.showConfirmDialog 1.4 PConfirm.java public class PConfirm extends JFrame { PConfirm(String title) { PConfirm frame = new PConfirm( " int answer = JOptionPane.showConfirmDialog(frame, " ", ", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(frame, " ", ", JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(frame, " ", ", JOptionPane.INFORMATION_MESSAGE);

14 2 1.3.3 JOptionPane.showInputDialog JOptionPane.showInputDialog JOptionPane.showInputDialog null PInput.java public class PInput extends JFrame { PInput(String title) { PInput frame = new PInput( " String title = JOptionPane.showInputDialog(frame, " ", ", JOptionPane.PLAIN_MESSAGE); if (title!= null) frame.settitle(title); 2 2.1 2.1.1 (0, 0) x y (200, 150) 200 150 2.1.2 (repaint) paint

2.1. 15 paint public void paint(graphics g) { super.paint(g); paint Graphics Graphics Swing AWT java.awt 2.1.3 Graphics filloval filloval 4 1 2 x y 3 4 g Graphics g.filloval(100, 80, 200, 140); (100, 80) 200 140 PFillOval.java public class PFillOval extends JFrame { PFillOval(String title) { public void paint(graphics g) { super.paint(g); g.filloval(100, 80, 200, 140); PFillOval frame = new PFillOval( " 2.1.4 getwidth getheight getwidth getheight PSize.java

16 2 public class PSize extends JFrame { final static int RADIUS = 50; PSize(String title) { public void paint(graphics g) { super.paint(g); g.filloval(getwidth()/2 - RADIUS, getheight()/2 - RADIUS, RADIUS*2, RADIUS*2); PSize frame = new PSize(" 2.1.5 (inset) getinsets Insets top bottom left right 4 PInsets.java public class PInsets extends JFrame { PInsets(String title) { public void paint(graphics g) { super.paint(g); Insets insets = getinsets(); g.filloval(insets.left, insets.top, 150, 150); PInsets frame = new PInsets(" 2.2 2.2.1

2.2. 17 Graphics Color AWT Graphics Graphics setcolor Color 2.2.2 Color Color new Color black cyan gray lightgray orange red yellow blue darkgray green magenta pink white 13 g.setcolor(color.orange); g Graphics POrange.java public class POrange extends JFrame { POrange(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(color.orange); g.filloval(100, 80, 200, 150); POrange frame = new POrange(" 2.2.3 Color Color Color 3 0 255 new Color(0, 0, 255) 0 0 255 PRGB.java

18 2 drawline drawrect fillrect drawroundrect fillroundrect drawoval filloval drawarc fillarc 2.1: public class PRGB extends JFrame { PRGB(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 255)); // g.filloval(30, 50, 200, 150); g.setcolor(new Color(128, 255, 0)); // g.filloval(90, 80, 200, 150); g.setcolor(new Color(192, 0, 255)); // g.filloval(150, 110, 200, 150); PRGB frame = new PRGB(" 2.3 2.3.1 Graphics filloval 2.1 2.3.2 drawline drawline 4 1 2 x y 3 4 x y g.drawline(100, 200, 300, 150); drawline (100, 200) (300, 150) PLine.java

2.3. 19 public class PLine extends JFrame { PLine(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(color.red); g.drawline(50, 60, 350, 260); PLine frame = new PLine(" 2.3.3 Graphics drawrect drawrect 4 2 x y 3 4 g.drawrect(60, 50, 200, 150); drawrect (60, 50) 200 150 fillrect 4 4 drawrect drawroundrect fillroundrect 6 4 drawrect 5 6 5 6 g.drawroundrect(100, 150, 200, 300, 40, 80); drawroundrect 40 80 PRect.java public class PRect extends JFrame { PRect(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(color.green); g.drawrect(40, 50, 140, 100); g.fillrect(220, 50, 140, 100); g.drawroundrect(40, 170, 140, 100, 50, 30); g.fillroundrect(220, 170, 140, 100, 50, 30);

20 2 PRect frame = new PRect(" 2.3.4 drawarc fillarc drawarc fillarc 6 4 drawrect 5 6 5 0 6 0 g.drawarc(50, 30, 200, 150, 90, -135); drawarc (50, 30) 200 150 PArc.java public class PArc extends JFrame { PArc(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(color.blue); g.drawarc(20, 50, 200, 120, 225, 270); g.fillarc(180, 140, 200, 120, -45, -270); PArc frame = new PArc(" 2.4 2.4.1 Graphics drawstring drawstring 3 1 2 3 x y g.drawstring(" ", 100, 70); drawstring (100, 70)

2.4. 21 PString.java public class PString extends JFrame { PString(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 0)); g.drawstring(" ", 160, 140); PString frame = new PString(" 2.4.2 Graphics drawstring Graphics Graphics setfont setfont Font Font 3 1 (logical font name) Java Serif SansSerif Monospaced 2 Font Font.PLAIN Font.ITALIC Font.BOLD Font.ITALIC Font.BOLD 3 new Font("SansSerif", Font.ITALIC, 32) SansSerif 32 PFont.java

22 2 public class PFont extends JFrame { PFont(String title) { public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 128)); g.setfont(new Font("Serif", Font.PLAIN, 28)); g.drawstring("serif PLAIN", 30, 70); g.setfont(new Font("Serif", Font.ITALIC, 28)); g.drawstring("serif ITALIC", 30, 110); g.setfont(new Font("Serif", Font.BOLD, 28)); g.drawstring("serif BOLD", 30, 150); g.setfont(new Font("Serif", Font.ITALIC Font.BOLD, 28)); g.drawstring("serif ITALIC BOLD", 30, 190); g.setfont(new Font("SansSerif", Font.PLAIN, 28)); g.drawstring("sansserif PLAIN", 30, 230); g.setfont(new Font("Monospaced", Font.PLAIN, 28)); g.drawstring("monospaced PLAIN", 30, 270); PFont frame = new PFont(" 2.5 2.5.1 Swing ImageIcon ImageIcon new ImageIcon("namako.jpg") namako.jpg 2.5.2 Graphics drawimage drawimage(image, int, int, int, int, ImageObserver) 1 drawimage ImageIcon Image ImageIcon Image getimage 2 5 2 x 3 y 4 5

6 this icon ImageIcon g.drawimage(icon.getimage(), 200, 100, 360, 280, this); (200, 100) 360 280 PImage.java public class PImage extends JFrame { ImageIcon icon; PImage(String title) { icon = new ImageIcon("sample.jpg public void paint(graphics g) { super.paint(g); g.drawimage(icon.getimage(), 100, 70, 200, 150, this); 23 PImage frame = new PImage(" 3 3.1 3.1.1 GUI (event) (event-driven) 3.1.2 GUI Java (event source) Java (event listener) Java

24 3 3.1.3 (listener interface) (adapter class) java.awt.event 3.1.4 (event processing method) 3.2 3.2.1 Java addmouselistener MouseAdapter mouseclicked MouseEvent addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { JOptionPane.showMessageDialog(null, " ", " ", JOptionPane.INFORMATION_MESSAGE);

3.2. 25 ); mouseclicked PClick.java import java.awt.event.*; public class PClick extends JFrame { PClick(String title) { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { JOptionPane.showMessageDialog(null, " ", " ", JOptionPane.INFORMATION_MESSAGE); ); PClick frame = new PClick(" 3.2.2 repaint 1 PRepaint.java import java.awt.event.*; public class PRepaint extends JFrame { int count; PRepaint(String title) { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { count++; repaint(); ); count = 0; public void paint(graphics g) { super.paint(g);

26 3 g.setcolor(color.blue); g.setfont(new Font("Serif", Font.PLAIN, 128)); g.drawstring(integer.tostring(count), 70, 180); PRepaint frame = new PRepaint(" 3.2.3 MouseEvent getpoint getpoint Point x y int x y PPoint.java import java.awt.event.*; public class PPoint extends JFrame { Point clicked; PPoint(String title) { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { clicked = e.getpoint(); repaint(); ); clicked = new Point(-100, -100); public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 0, 128)); g.filloval(clicked.x - 10, clicked.y - 10, 20, 20); PPoint frame = new PPoint(" 3.2.4 Swing SwingUtilities SwingUtilities isleftmousebutton

3.2. 27 ismiddlemousebutton isrightmousebutton e SwingUtilities.isLeftMouseButton(e) PMouseButton.java import java.awt.event.*; public class PMouseButton extends JFrame { final static int Y = 140; final static int STEP = 20; final static int SIZE = 30; int x; PMouseButton(String title) { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { if (SwingUtilities.isLeftMouseButton(e)) x -= STEP; else if (SwingUtilities.isRightMouseButton(e)) x += STEP; repaint(); ); x = 185; public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 128)); g.filloval(x, Y, SIZE, SIZE); PMouseButton frame = new PMouseButton( " 3.2.5 addmousemotionlistener MouseMotionAdapter mousemoved MouseEvent

28 3 addmousemotionlistener(new MouseMotionAdapter() { public void mousemoved(mouseevent e) { position = e.getpoint(); repaint(); ); mousemoved position PMouseMotion.java import java.awt.event.*; public class PMouseMotion extends JFrame { Point position; PMouseMotion(String title) { addmousemotionlistener(new MouseMotionAdapter() { public void mousemoved(mouseevent e) { position = e.getpoint(); repaint(); ); position = new Point(0, 0); public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 0)); g.setfont(new Font("Serif", Font.PLAIN, 58)); g.drawstring( "(" + position.x + "," + position.y + ")", 60, 110); PMouseMotion frame = new PMouseMotion(" 3.3 3.3.1 addkeylistener KeyAdapter keypressed

3.3. 29 KeyEvent addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { JOptionPane.showMessageDialog(null, " ", " ", JOptionPane.INFORMATION_MESSAGE); ); keypressed PKey.java import java.awt.event.*; public class PKey extends JFrame { PKey(String title) { addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { JOptionPane.showMessageDialog(null, " ", " ", JOptionPane.INFORMATION_MESSAGE); ); PKey frame = new PKey(" 3.3.2 (key code) getkeycode KeyEvent VK_ 6 VK_6 M VK_M VK_COMMA VK_SHIFT VK_ENTER VK_UP 1 (F1) VK_F1

30 3 PKeyCode.java import java.awt.event.*; public class PKeyCode extends JFrame { final static int Y = 140; final static int STEP = 20; final static int SIZE = 30; int x; PKeyCode(String title) { addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { int kc = e.getkeycode(); if (kc == KeyEvent.VK_LEFT) x -= STEP; else if (kc == KeyEvent.VK_RIGHT) x += STEP; repaint(); ); x = 185; public void paint(graphics g) { super.paint(g); g.setcolor(new Color(128, 0, 255)); g.filloval(x, Y, SIZE, SIZE); PKeyCode frame = new PKeyCode(" 3.3.3 KeyEvent getkeytext PKeyText.java import java.awt.event.*; public class PKeyText extends JFrame { String pressed; PKeyText(String title) { addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { pressed = KeyEvent.getKeyText(e.getKeyCode()); repaint();

3.4. 31 ); pressed = ""; public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 0, 128)); g.setfont(new Font("Serif", Font.PLAIN, 58)); g.drawstring(pressed, 60, 110); PKeyText frame = new PKeyText(" 3.4 3.4.1 addwindowlistener WindowAdapter WindowEvent windowopened windowclosing windowclosed windowactivated windowdeactivated windowiconified windowdeiconified 3.4.2 setdefaultcloseoperation setdefaultcloseoperation(jframe.do_nothing_on_close); System.exit(0);

32 3 exit PConfirmExit.java import java.awt.event.*; public class PConfirmExit extends JFrame { PConfirmExit(String title) { setdefaultcloseoperation(jframe.do_nothing_on_close); addwindowlistener(new WindowAdapter() { public void windowclosing(windowevent e) { confirmexit(); ); void confirmexit() { int answer = JOptionPane.showConfirmDialog(null, " ", ", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) System.exit(0); PConfirmExit frame = new PConfirmExit( " 3.5 3.5.1 (timer) 3.5.2 Swing Timer Timer 2 1 2 ActionListener actionperformed ActionEvent

3.5. 33 timer = new Timer(1000, new ActionListener() { count++; repaint(); ); timer 1000 1 count 3.5.3 start stop isrunning isrunning 1 1 PTimer.java import java.awt.event.*; public class PTimer extends JFrame { int count; Timer timer; PTimer(String title) { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { if (timer.isrunning()) timer.stop(); else { count = 0; timer.start(); repaint(); ); timer = new Timer(1000, new ActionListener() { count++; repaint(); ); count = 0; public void paint(graphics g) { super.paint(g); g.setcolor(new Color(0, 128, 128)); g.setfont(new Font("Serif", Font.PLAIN, 128)); g.drawstring(integer.tostring(count), 70, 180);

34 3 PTimer frame = new PTimer(" 3.6 3.6.1 (character) Character AWT Rectangle class Character extends Rectangle {... Rectangle x y width height 4 x y Color color; Rectangle 4 x y width height Character Character(int x, int y, int width, int height, Color acolor) { super(x, y, width, height); color = acolor; 3.6.2 inside inside final static int INSIDE = 0; final static int EAST = 1; final static int WEST = 2; final static int SOUTH = 3;

3.6. 35 final static int NORTH = 4; inside int inside(rectangle area) { if (x < area.x) return WEST; else if (x + width > area.x + area.width) return EAST; else if (y < area.y) return NORTH; else if (y + height > area.y + area.height) return SOUTH; else return INSIDE; 3.6.3 (collision detection) collision Character Rectangle intersection isempty intersection isempty collision boolean collision(character character) { return! intersection(character).isempty(); collision 3.6.4 PTennis.java import java.awt.event.*; class Character extends Rectangle { final static int INSIDE = 0; final static int EAST = 1; final static int WEST = 2; final static int SOUTH = 3;

36 3 final static int NORTH = 4; Color color; Character(int x, int y, int width, int height, Color acolor) { super(x, y, width, height); color = acolor; int inside(rectangle area) { if (x < area.x) return WEST; else if (x + width > area.x + area.width) return EAST; else if (y < area.y) return NORTH; else if (y + height > area.y + area.height) return SOUTH; else return INSIDE; boolean collision(character character) { return! intersection(character).isempty(); class Ball extends Character { final static int SOUTHEAST = 0; final static int SOUTHWEST = 1; final static int NORTHWEST = 2; final static int NORTHEAST = 3; Rectangle court; int velocity, direction; Ball(Rectangle acourt, int size, int avelocity, Color color) { super(0, 0, size, size, color); court = acourt; velocity = avelocity; reset(); void reset() { setlocation(court.x, court.y); direction = SOUTHEAST; void forward(int ax, int ay) { setlocation(x + velocity * ax, y + velocity * ay); void directionforward() { switch (direction) { case SOUTHEAST: forward(1, 1); break; case SOUTHWEST: forward(-1, 1); break; case NORTHWEST: forward(-1, -1); break; case NORTHEAST: forward(1, -1);

3.6. 37 break; void turn() { switch (inside(court)) { case WEST: if (direction == SOUTHWEST) direction = SOUTHEAST; else if (direction == NORTHWEST) direction = NORTHEAST; break; case EAST: if (direction == SOUTHEAST) direction = SOUTHWEST; else if (direction == NORTHEAST) direction = NORTHWEST; break; case NORTH: if (direction == NORTHEAST) direction = SOUTHEAST; else if (direction == NORTHWEST) direction = SOUTHWEST; break; void hit() { if (direction == SOUTHEAST) direction = NORTHEAST; else if (direction == SOUTHWEST) direction = NORTHWEST; boolean south() { return direction == SOUTHEAST direction == SOUTHWEST; boolean beyondsouth() { return inside(court) == SOUTH; void draw(graphics g) { g.setcolor(color); g.filloval(x, y, width, height); class Racket extends Character { Racket(Rectangle court, int width, int height, Color color) { super(court.x + (court.width/2 - width/2), court.y + (court.height - height*2), width, height, color); void setx(int ax) { x = ax - width/2; void draw(graphics g) { g.setcolor(color); g.fillrect(x, y, width, height);

38 3 public class PTennis extends JFrame { final static int FRAME_X = 400; final static int FRAME_Y = 300; int score; Rectangle court; Ball ball; Racket racket; Timer timer; PTennis(String title) { setsize(frame_x, FRAME_Y); addmousemotionlistener(new MouseMotionAdapter() { public void mousemoved(mouseevent e) { racket.setx(e.getpoint().x); repaint(); ); score = 0; Insets insets = getinsets(); court = new Rectangle(insets.left, insets.top, FRAME_X - (insets.left + insets.right), FRAME_Y - (insets.top + insets.bottom)); ball = new Ball(court, 20, 10, new Color(128, 0, 0)); racket = new Racket(court, 60, 15, new Color(0, 0, 128)); timer = new Timer(10, new ActionListener() { background(); ); timer.start(); void gameover() { JOptionPane.showMessageDialog(this, " ", " ", JOptionPane.INFORMATION_MESSAGE); score = 0; ball.reset(); void background() { if (ball.beyondsouth()) gameover(); ball.turn(); if (ball.south() && ball.collision(racket)) { ball.hit(); score++; ball.directionforward(); repaint(); void drawscore(graphics g) { g.setcolor(new Color(0, 128, 0)); g.setfont(new Font("Serif", Font.PLAIN, 32)); g.drawstring(integer.tostring(score), court.x + 20, court.y + 40); public void paint(graphics g) { super.paint(g);

39 drawscore(g); ball.draw(g); racket.draw(g); PTennis frame = new PTennis(" 4 4.1 4.1.1 (button) (push button) (check box) (radio button) 4.1.2 Swing JButton JButton JButton button = new JButton(" button settext 4.1.3 (container) AWT Container JFrame Container (add) add container component container.add(component); component container

40 4 4.1.4 JFrame Container (content pane) getcontentpane Container JPanel JPanel frame (JPanel)frame.getContentPane() 4.1.5 Java (layout manager) setlayout (flow layout) FlowLayout container container.setlayout(new FlowLayout()); PButton.java public class PButton extends JFrame { PButton(String title) { JPanel content = (JPanel)getContentPane(); content.setlayout(new FlowLayout()); content.add(new JButton(" ")); PButton frame = new PButton("

4.2. 41 4.1.6 Java addactionlistener ActionListener actionperformed ActionEvent PAction.java import java.awt.event.*; public class PAction extends JFrame { PAction(String title) { JPanel content = (JPanel)getContentPane(); content.setlayout(new FlowLayout()); JButton button = new JButton(" button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, " ", " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button); PAction frame = new PAction(" 4.2 4.2.1 Java (layout manager)

42 4 setlayout 4.2.2 (border layout) BorderLayout 1 (layout constraints) add add add BorderLayout NORTH SOUTH WEST EAST CENTER container component container.add(component, BorderLayout.EAST); component container PBorder.java public class PBorder extends JFrame { PBorder(String title) { JPanel content = (JPanel)getContentPane(); content.add(new JButton(" "), BorderLayout.NORTH); content.add(new JButton(" "), BorderLayout.SOUTH); content.add(new JButton(" "), BorderLayout.EAST); content.add(new JButton(" "), BorderLayout.WEST); content.add(new JButton(" "), BorderLayout.CENTER); PBorder frame = new PBorder(" 4.2.3 (grid layout)

4.2. 43 GridLayout 2 1 2 new GridLayout(7, 5) 7 5 PGrid.java public class PGrid extends JFrame { PGrid(String title) { JPanel content = (JPanel)getContentPane(); content.setlayout(new GridLayout(4, 5)); for (int i = 1; i <= 18; i++) { content.add(new JButton(Integer.toString(i))); PGrid frame = new PGrid(" 4.2.4 (panel) JPanel new JPanel(new BorderLayout()) 1 4 4 1 PPanel.java public class PPanel extends JFrame { PPanel(String title) {

44 4 JPanel content = (JPanel)getContentPane(); content.setlayout(new GridLayout(1, 2)); JPanel left = new JPanel(new GridLayout(1, 4)); for (int i = 1; i <= 4; i++) { left.add(new JButton(Integer.toString(i))); content.add(left); JPanel right = new JPanel(new GridLayout(4, 1)); for (int i = 5; i <= 8; i++) { right.add(new JButton(Integer.toString(i))); content.add(right); PPanel frame = new PPanel(" 4.3 4.3.1 (check box) (radio button) (toggle button) 4.3.2 Swing JCheckBox JRadioButton 1 new JCheckBox(" ") 1 new JRadioButton(" ", true) 2 2 4.3.3 isselected setselected PCheckBox.java

4.3. 45 import java.awt.event.*; public class PCheckBox extends JFrame { JCheckBox check; PCheckBox(String title) { JPanel content = (JPanel)getContentPane(); content.setlayout(new FlowLayout()); check = new JCheckBox(" content.add(check); JButton button = new JButton(" button.addactionlistener(new ActionListener() { String apology = " "; if (check.isselected()) apology += "(^^ "; JOptionPane.showMessageDialog(null, apology, " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button); PCheckBox frame = new PCheckBox(" 4.3.4 Swing (button group) ButtonGroup new ButtonGroup() add group button group.add(button); button group PRadioButton.java import java.awt.event.*; public class PRadioButton extends JFrame { final static int RADIONUM = 3;

46 4 JRadioButton[] radio; PRadioButton(String title) { JPanel content = (JPanel)getContentPane(); content.setlayout(new FlowLayout()); ButtonGroup group = new ButtonGroup(); radio = new JRadioButton[RADIONUM]; radio[0] = new JRadioButton(" ", true); radio[1] = new JRadioButton(" radio[2] = new JRadioButton(" for (int i = 0; i < RADIONUM; i++) { group.add(radio[i]); content.add(radio[i]); JButton button = new JButton(" button.addactionlistener(new ActionListener() { int num = 0; boolean notfound = true; for (int i = 0; notfound && i < RADIONUM; i++) { if (radio[i].isselected()) { num = i; notfound = false; String salute = ""; switch (num) { case 0: salute = " "; break; case 1: salute = " "; break; case 2: salute = " "; break; JOptionPane.showMessageDialog(null, salute, " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button); PRadioButton frame = new PRadioButton(" 4.4 4.4.1 (menu bar) Swing JMenuBar

4.4. 47 new JMenuBar() setjmenubar frame menubar frame.setjmenubar(menubar); menubar frame 4.4.2 Swing JMenu 1 new JMenu(" ") add menubar menu menubar.add(menu); menu menubar 4.4.3 (menu item) Swing JMenuItem JCheckBoxMenuItem JRadioButtonMenuItem 1 new JMenuItem(" ") add menu item menu.add(item); item menu 4.4.4 (separator) addseparator menu menu.addseparator();

48 4 menu 4.4.5 (submenu) PMenu.java import java.awt.event.*; public class PMenu extends JFrame { PMenu(String title) { JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu food = new JMenu(" menubar.add(food); JMenuItem curry = new JMenuItem(" curry.addactionlistener(new ActionListener() { showorder(" ); food.add(curry); JMenu teishoku = new JMenu(" food.add(teishoku); JMenuItem tonkatsu = new JMenuItem(" tonkatsu.addactionlistener(new ActionListener() { showorder(" ); teishoku.add(tonkatsu); JMenuItem yakiniku = new JMenuItem(" yakiniku.addactionlistener(new ActionListener() { showorder(" ); teishoku.add(yakiniku); JMenu drink = new JMenu(" menubar.add(drink); JMenuItem oolong = new JMenuItem(" oolong.addactionlistener(new ActionListener() { showorder(" ); drink.add(oolong); JMenuItem coffee = new JMenuItem(" coffee.addactionlistener(new ActionListener() { showorder(" ); drink.add(coffee); drink.addseparator(); JMenuItem water = new JMenuItem(" water.addactionlistener(new ActionListener() {

4.4. 49 showorder(" ); drink.add(water); void showorder(string order) { JOptionPane.showMessageDialog(null, order + " ", ", JOptionPane.INFORMATION_MESSAGE); PMenu frame = new PMenu(" 4.4.6 setenabled PEnabled.java import java.awt.event.*; public class PEnabled extends JFrame { JCheckBoxMenuItem toggle; JMenuItem item; PEnabled(String title) { JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu menu = new JMenu(" menubar.add(menu); toggle = new JCheckBoxMenuItem( " toggle.addactionlistener(new ActionListener() { if (toggle.isselected()) item.setenabled(true); else item.setenabled(false); ); menu.add(toggle); item = new JMenuItem( " item.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, " ", " ", ); item.setenabled(false); menu.add(item); JOptionPane.INFORMATION_MESSAGE);

50 4 PEnabled frame = new PEnabled( " 4.4.7 (mnemonic) (short cut) Alt setmnemonic item item.setmnemonic(keyevent.vk_m); M 4.4.8 (accelerator) setaccelerator KeyStroke KeyStroke new getkeystroke 2 1 2 (modifier) InputEvent.CTRL_MASK item item.setaccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); A PMnemonic.java import java.awt.event.*; public class PMnemonic extends JFrame { PMnemonic(String title) {

4.5. 51 JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu food = new JMenu(" (F) food.setmnemonic(keyevent.vk_f); menubar.add(food); JMenuItem chuudon = new JMenuItem(" (C) chuudon.setmnemonic(keyevent.vk_c); chuudon.setaccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); chuudon.addactionlistener(new ActionListener() { showorder(" ); food.add(chuudon); JMenuItem yakisoba = new JMenuItem(" (Y) yakisoba.setmnemonic(keyevent.vk_y); yakisoba.setaccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK)); yakisoba.addactionlistener(new ActionListener() { showorder(" ); food.add(yakisoba); void showorder(string order) { JOptionPane.showMessageDialog(null, order + " ", ", JOptionPane.INFORMATION_MESSAGE); PMnemonic frame = new PMnemonic( " 4.5 4.5.1 (popup menu) Swing JPopupMenu add 4.5.2 show 3

52 5 1 getcomponent show 2 3 x y PPopupMenu.java import java.awt.event.*; public class PPopupMenu extends JFrame { JPopupMenu popup; PPopupMenu(String title) { createpopupmenu(); addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { Point p = e.getpoint(); popup.show(e.getcomponent(), p.x, p.y); ); void showorder(string order) { JOptionPane.showMessageDialog(null, order + " ", ", JOptionPane.INFORMATION_MESSAGE); void createpopupmenu() { popup = new JPopupMenu(); JMenuItem yakisoba = new JMenuItem(" yakisoba.addactionlistener(new ActionListener() { showorder(" ); popup.add(yakisoba); JMenuItem chuudon = new JMenuItem(" chuudon.addactionlistener(new ActionListener() { showorder(" ); popup.add(chuudon); JMenuItem nirareba = new JMenuItem(" nirareba.addactionlistener(new ActionListener() { showorder(" ); popup.add(nirareba); PPopupMenu frame = new PPopupMenu( "

5.1. 53 5 5.1 5.1.1 (string) (text) Swing Swing 5.1.2 (label) Swing JLabel 1 PLabel.java public class PLabel extends JFrame { PLabel(String title) { JPanel content = (JPanel)getContentPane(); content.add(new JLabel(" "), BorderLayout.CENTER); PLabel frame = new PLabel(" 5.1.3 settext PSetText.java import java.awt.event.*; public class PSetText extends JFrame { JLabel label; PSetText(String title) { JPanel content = (JPanel)getContentPane(); label = new JLabel(" content.add(label, BorderLayout.CENTER);

54 5 JButton button = new JButton(" button.addactionlistener(new ActionListener() { String text = JOptionPane.showInputDialog( null, " ", " ", JOptionPane.PLAIN_MESSAGE); if (text!= null) label.settext(text); ); content.add(button, BorderLayout.SOUTH); PSetText frame = new PSetText(" 5.2 HTML 5.2.1 HTML Swing HTML HTML HTML settext HTML PHTML.java public class PHTML extends JFrame { PHTML(String title) { JPanel content = (JPanel)getContentPane(); content.add(new JLabel( "<html><head></head><body><table border=\"1\">" + "<tr><th> </th><th> </th></tr>" + "<tr><td> </td><td>8 14 </td></tr>" + "<tr><td> </td><td>12 26 </td></tr>" + "</table></body></html>"), BorderLayout.CENTER); PHTML frame = new PHTML( " HTML 5.2.2 HTML Swing CSS HTML style head style

5.3. 55 PStylesheet.java public class PStylesheet extends JFrame { PStylesheet(String title) { JPanel content = (JPanel)getContentPane(); content.add(new JLabel( "<html><head><style type=\"text/css\">" + "p { font-size: 16pt; color: #000099 " + ".emphasis { font-size: 32pt; color: #ff0099; " + " background-color: #ffffcc " + ".border { border-style: solid; " + " border-width: thick " + "</style></head><body>" + "<p> </p>" + "<p class=\"emphasis\"> </p>" + "<p class=\"border\"> </p>" + "</body></html>"), BorderLayout.CENTER); PStylesheet frame = new PStylesheet(" 5.3 5.3.1 1 (text field) Swing JTextField gettext settext (focus) requestfocusinwindow PTextField.java import java.awt.event.*; public class PTextField extends JFrame { JTextField field; JLabel label;

56 5 PTextField(String title) { JPanel content = (JPanel)getContentPane(); field = new JTextField(); content.add(field, BorderLayout.NORTH); label = new JLabel(" content.add(label, BorderLayout.CENTER); JButton button = new JButton(" button.addactionlistener(new ActionListener() { label.settext(field.gettext()); field.requestfocusinwindow(); ); content.add(button, BorderLayout.SOUTH); field.requestfocusinwindow(); PTextField frame = new PTextField( " 5.3.2 ActionListener actionperformed PEnterKey.java import java.awt.event.*; public class PEnterKey extends JFrame { JTextField field; JLabel label; PEnterKey(String title) { JPanel content = (JPanel)getContentPane(); field = new JTextField(); field.addactionlistener(new ActionListener() { label.settext(field.gettext()); field.requestfocusinwindow(); ); content.add(field, BorderLayout.NORTH); label = new JLabel(" content.add(label, BorderLayout.CENTER); field.requestfocusinwindow(); PEnterKey frame = new PEnterKey( "

5.4. 57 5.4 5.4.1 (text area) Swing JTextArea gettext settext requestfocusinwindow setlinewrap PTextArea.java import java.awt.event.*; public class PTextArea extends JFrame { JTextArea area; PTextArea(String title) { JPanel content = (JPanel)getContentPane(); area = new JTextArea(); area.setlinewrap(true); content.add(area, BorderLayout.CENTER); JButton button = new JButton( " button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, area.gettext(), " ", JOptionPane.INFORMATION_MESSAGE); area.requestfocusinwindow(); ); content.add(button, BorderLayout.SOUTH); area.requestfocusinwindow(); PTextArea frame = new PTextArea(" 5.4.2 (scroll) (scroll bar) Swing

58 5 (scroll pane) JScrollPane 1 comp new JScrollPane(comp) comp PScrollPane.java public class PScrollPane extends JFrame { PScrollPane(String title) { JPanel content = (JPanel)getContentPane(); JTextArea area = new JTextArea(); JScrollPane scroll = new JScrollPane(area); content.add(scroll, BorderLayout.CENTER); area.requestfocusinwindow(); PScrollPane frame = new PScrollPane( " 5.4.3 (clip board) cut copy paste cut copy cut copy paste PEditor.java import java.awt.event.*; import java.io.*; public class PEditor extends JFrame { JTextArea area; String path; PEditor(String title, String apath) { path = apath;

5.4. 59 JPanel content = (JPanel)getContentPane(); area = new JTextArea(); area.setlinewrap(true); JScrollPane scroll = new JScrollPane(area); content.add(scroll, BorderLayout.CENTER); JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu mfile = new JMenu(" menubar.add(mfile); JMenuItem isave = new JMenuItem(" isave.addactionlistener(new ActionListener() { save(); ); mfile.add(isave); JMenu medit = new JMenu(" menubar.add(medit); JMenuItem icut = new JMenuItem(" icut.addactionlistener(new ActionListener() { area.cut(); ); medit.add(icut); JMenuItem icopy = new JMenuItem(" icopy.addactionlistener(new ActionListener() { area.copy(); ); medit.add(icopy); JMenuItem ipaste = new JMenuItem(" ipaste.addactionlistener(new ActionListener() { area.paste(); ); medit.add(ipaste); load(); area.requestfocusinwindow(); void showerror(string message) { JOptionPane.showMessageDialog(null, message, " ", JOptionPane.ERROR_MESSAGE); void load() { try { BufferedReader reader = new BufferedReader( new FileReader(path)); String buffer = ""; String line; while ((line = reader.readline())!= null) buffer += line + \n ; reader.close(); area.settext(buffer); catch (IOException e) { showerror(e.getmessage()); void save() { try {

60 5 BufferedWriter writer = new BufferedWriter( new FileWriter(path)); String buffer = area.gettext(); for (int i = 0; i < buffer.length(); i++) if (buffer.charat(i) == \n ) writer.newline(); else writer.write((int)buffer.charat(i)); writer.close(); catch (IOException e) { showerror(e.getmessage()); if (args.length >= 1) { PEditor frame = new PEditor( " ", args[0]); else { System.out.println(" : java PEditor 5.5 5.5.1 Swing (editor pane) JEditorPane 5.5.2 RFC2045 (media type) setcontenttype text/plain text/html text/rtf HTML(hypertext markup language) RTF(rich text format) gettext PEditorPane.java import java.awt.event.*; public class PEditorPane extends JFrame { JEditorPane editor; PEditorPane(String title) {

5.5. 61 JPanel content = (JPanel)getContentPane(); editor = new JEditorPane(); editor.setcontenttype("text/html content.add(new JScrollPane(editor), BorderLayout.CENTER); JButton button = new JButton( " button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, editor.gettext(), " ", JOptionPane.INFORMATION_MESSAGE); editor.requestfocusinwindow(); ); content.add(button, BorderLayout.SOUTH); editor.requestfocusinwindow(); PEditorPane frame = new PEditorPane( " 5.5.3 URL setpage URL setpage URL IOException 5.5.4 seteditable javax.swing.event addhyperlinklistener HyperlinkListener hyperlinkupdate HyperlinkEvent geteventtype HyperlinkEvent.EventType HyperlinkEvent.EventType.ENTERED HyperlinkEvent.EventType.EXITED

62 5 HyperlinkEvent.EventType.ACTIVATED geturl URL geturl URL URL tostring PBrowser.java import javax.swing.event.*; import java.awt.event.*; import java.io.*; public class PBrowser extends JFrame { JTextField urlfield; JEditorPane webpage; JLabel status; PBrowser(String title) { JPanel content = (JPanel)getContentPane(); JPanel north = new JPanel(new BorderLayout()); urlfield = new JTextField(); north.add(urlfield, BorderLayout.CENTER); JButton move = new JButton(" move.addactionlistener(new ActionListener() { movepage(urlfield.gettext()); ); north.add(move, BorderLayout.EAST); content.add(north, BorderLayout.NORTH); createwebpage(); content.add(new JScrollPane(webpage), BorderLayout.CENTER); status = new JLabel(" content.add(status, BorderLayout.SOUTH); void hyperlink(hyperlinkevent e) { HyperlinkEvent.EventType type = e.geteventtype(); String url = e.geturl().tostring(); if (type == HyperlinkEvent.EventType.ACTIVATED) movepage(url); else if (type == HyperlinkEvent.EventType.ENTERED) status.settext(url); else if (type == HyperlinkEvent.EventType.EXITED) status.settext(" void createwebpage() { webpage = new JEditorPane(); webpage.seteditable(false); webpage.setcontenttype("text/html webpage.addhyperlinklistener(new HyperlinkListener() { public void hyperlinkupdate(hyperlinkevent e) { hyperlink(e); ); void movepage(string url) {

63 if (url.length() > 0) { try { webpage.setpage(url); urlfield.settext(url); catch (IOException e) { JOptionPane.showMessageDialog(this, " ", " ", JOptionPane.ERROR_MESSAGE); PBrowser frame = new PBrowser(" 6 6.1 6.1.1 1 (list) (item) Swing JList PList.java public class PList extends JFrame { PList(String title) { JPanel content = (JPanel)getContentPane(); String[] items = new String[40]; for (int i = 0; i < 40; i++) items[i] = "item" + i; JList list = new JList(items); JScrollPane scroll = new JScrollPane(list); content.add(scroll, BorderLayout.CENTER); PList frame = new PList(" 6.1.2 getselectedvalues

64 6 PGetSelected.java import java.awt.event.*; public class PGetSelected extends JFrame { JList list; PGetSelected(String title) { JPanel content = (JPanel)getContentPane(); String[] items = new String[40]; for (int i = 0; i < 40; i++) items[i] = "item" + i; list = new JList(items); JScrollPane scroll = new JScrollPane(list); content.add(scroll, BorderLayout.CENTER); JButton button = new JButton(" button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, list.getselectedvalues(), " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button, BorderLayout.SOUTH); PGetSelected frame = new PGetSelected( " 6.2 6.2.1 (list model) DefaultListModel JList DefaultListModel addelement 1 PListModel.java import java.awt.event.*; public class PListModel extends JFrame {

6.2. 65 JList list; PListModel(String title) { JPanel content = (JPanel)getContentPane(); DefaultListModel model = new DefaultListModel(); for (int i = 0; i < 40; i++) model.addelement("item" + i); list = new JList(model); JScrollPane scroll = new JScrollPane(list); content.add(scroll, BorderLayout.CENTER); JButton button = new JButton(" button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, list.getselectedvalues(), " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button, BorderLayout.SOUTH); PListModel frame = new PListModel(" 6.2.2 (index) 0 getselectedindex getselectedindices index 1 6.2.3 DefaultListModel insertelementat 2 1 2 removeelementat 2 PAddRemove.java

66 6 import java.awt.event.*; public class PAddRemove extends JFrame { DefaultListModel model; JList list; PAddRemove(String title) { JPanel content = (JPanel)getContentPane(); model = new DefaultListModel(); for (int i = 0; i < 40; i++) model.addelement("item" + i); list = new JList(model); JScrollPane scroll = new JScrollPane(list); content.add(scroll, BorderLayout.CENTER); JPanel south = new JPanel(new GridLayout(1, 2)); JButton addbutton = new JButton(" addbutton.addactionlistener(new ActionListener() { String item = JOptionPane.showInputDialog( null, " ", " ", JOptionPane.PLAIN_MESSAGE); if (item!= null) { int index = list.getselectedindex(); if (index == -1) model.addelement(item); else model.insertelementat(item, index); ); south.add(addbutton); JButton removebutton = new JButton(" removebutton.addactionlistener(new ActionListener() { int[] indices = list.getselectedindices(); for (int i = indices.length - 1; i >= 0; i--) { model.removeelementat(indices[i]); ); south.add(removebutton); content.add(south, BorderLayout.SOUTH); PAddRemove frame = new PAddRemove(" 6.3 6.3.1 (combo box)

6.3. 67 Swing JComboBox 6.3.2 getselecteditem getselectedindex setselectedindex setmaximumrowcount 0 1 6.3.3 seteditable PComboBox.java import java.awt.event.*; public class PComboBox extends JFrame { JComboBox combo; PComboBox(String title) { JPanel content = (JPanel)getContentPane(); String[] items = new String[40]; for (int i = 0; i < 40; i++) items[i] = "item" + i; combo = new JComboBox(items); combo.setselectedindex(7); combo.setmaximumrowcount(10); combo.seteditable(true); content.add(combo, BorderLayout.NORTH); JButton button = new JButton(" button.addactionlistener(new ActionListener() { JOptionPane.showMessageDialog(null, combo.getselecteditem(), " ", JOptionPane.INFORMATION_MESSAGE); ); content.add(button, BorderLayout.SOUTH); PComboBox frame = new PComboBox("

68 6 6.3.4 (combo box model) Swing DefaultComboBoxModel addelement insertelementat removeelementat PComboBoxModel.java import java.awt.event.*; public class PComboBoxModel extends JFrame { DefaultComboBoxModel model; JComboBox combo; PComboBoxModel(String title) { JPanel content = (JPanel)getContentPane(); String[] items = new String[40]; for (int i = 0; i < 40; i++) items[i] = "item" + i; model = new DefaultComboBoxModel(items); combo = new JComboBox(model); combo.setselectedindex(7); combo.setmaximumrowcount(10); content.add(combo, BorderLayout.NORTH); JPanel south = new JPanel(new GridLayout(1, 2)); JButton addbutton = new JButton(" addbutton.addactionlistener(new ActionListener() { String item = JOptionPane.showInputDialog( null, " ", " ", JOptionPane.PLAIN_MESSAGE); if (item!= null) { int index = combo.getselectedindex(); if (index == -1) model.addelement(item); else model.insertelementat(item, index); ); south.add(addbutton); JButton removebutton = new JButton(" removebutton.addactionlistener(new ActionListener() { int index = combo.getselectedindex(); if (index!= -1) model.removeelementat(index);