11-GUI.key

Similar documents
9장.key

11장.key

자바GUI실전프로그래밍2_장대원.PDF

Microsoft PowerPoint - 04-UDP Programming.ppt

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

강의자료

10장.key

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

public class FlowLayoutPractice extends JFrame { public FlowLayoutPractice() { super("flowlayout Practice"); this. Container contentpane = getcontentp

gnu-lee-oop-kor-lec10-1-chap10

Microsoft PowerPoint - EEL2 Lecture10 -Swing and Event Handling.pptx

05-class.key

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

iii. Design Tab 을 Click 하여 WindowBuilder 가자동으로생성한 GUI 프로그래밍환경을확인한다.

Java Programing Environment

제8장 자바 GUI 프로그래밍 II

Microsoft PowerPoint - 14주차 강의자료

12-file.key

Microsoft PowerPoint - ÀÚ¹Ù08Àå-1.ppt

PowerPoint 프레젠테이션

No Slide Title

Chap12

02 C h a p t e r Java

ch09

슬라이드 1

rmi_박준용_final.PDF

Microsoft PowerPoint - [JAVA프로그래밍]9장GUI

10 이벤트 처리와 그래픽 프로그래밍.key

슬라이드 1

Microsoft PowerPoint - ÀÚ¹Ù08Àå-2.ppt

<4D F736F F F696E74202D20C1A63236C0E520BED6C7C3B8B428B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint 프레젠테이션

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - Java-03.pptx

JMF3_심빈구.PDF

09-interface.key

PowerPoint Presentation

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

모든스윙컴포넌트에는텍스트옆에이미지를추가로표시할수있다. ImageIcon image = new ImageIcon("image.gif"); JLabel label = new JLabel(" 이미지레이블 "); label.seticon(image);

JTable과 MVC(Model-View-Controller) 구조 - 모델-뷰-컨트롤러구조는데이터의저장과접근에대한제공은모델이담당하고, 화면표시는뷰, 이벤트의처리는컨트롤러가하도록각역할을구분한구조이다. 즉, 역할의분담을통하여상호간의영향을최소화하고각요소의독립성을보장하여독자

비긴쿡-자바 00앞부속

Microsoft PowerPoint 자바-AWT컴포넌트(Ch8).pptx

Microsoft PowerPoint - java1-lecture11.ppt [호환 모드]

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - java1-lecture11.ppt [호환 모드]

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - java2-lecture5.ppt [호환 모드]

PowerPoint 프레젠테이션

Microsoft PowerPoint - Java7.pptx

JMF1_심빈구.PDF

Microsoft PowerPoint - java1-lecture10.ppt [호환 모드]

8장.그래픽 프로그래밍

PowerPoint 프레젠테이션

중간고사

JMF2_심빈구.PDF

5장.key

No Slide Title

10-Java Applet

Java Coding Standard

01-OOPConcepts(2).PDF

쉽게 풀어쓴 C 프로그래밍

歯제9장.PDF

Cluster management software

(Microsoft PowerPoint - java1-lecture7.ppt [\310\243\310\257 \270\360\265\345])

untitled

PowerPoint 프레젠테이션

Microsoft Word - java19-1-final-answer.doc

13ÀåÃß°¡ºÐ

Cluster management software

Interstage5 SOAP서비스 설정 가이드

DB 에데이터저장을위한입력창설계 - JLabel, JTextField, JButton 을이용한입력창설계 - 2 -

Microsoft PowerPoint - java2-lecture6.ppt [호환 모드]

Microsoft Word - java18-1-final-answer.doc

(Microsoft PowerPoint - java2-lecture6.ppt [\310\243\310\257 \270\360\265\345])

제8장 자바 GUI 프로그래밍 II

자바 프로그래밍

웹연동 } 웹 (Web) 환경에서데이터베이스시스템을연동하는방법은다음과같다 } Server Client 구조의통신 (2-Tier) } Server Middleware Client 구조의통신 (3-Tier) 2

(Microsoft PowerPoint - LZVNQBAJWGTC.ppt [\310\243\310\257 \270\360\265\345])

Orcad Capture 9.x

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

오버라이딩 (Overriding)

신림프로그래머_클린코드.key

Microsoft PowerPoint - 06-Chapter09-Event.ppt

자바로

PowerPoint 프레젠테이션

mytalk

슬라이드 1

DocsPin_Korean.pages

Connection 8 22 UniSQLConnection / / 9 3 UniSQL OID SET

07 자바의 다양한 클래스.key

ilist.add(new Integer(1))과 같이 사용하지 않고 ilist.add(1)과 같이 사용한 것은 자바 5.0에 추가된 기본 자료형과 해당 객체 자료 형과의 오토박싱/언박싱 기능을 사용한 것으로 오토박싱이란 자바 컴파일러가 객체를 요구하는 곳에 기본 자료형

MAX+plus II Getting Started - 무작정따라하기

PowerPoint Presentation

PowerPoint 프레젠테이션

Spring Boot/JDBC JdbcTemplate/CRUD 예제

제11장 프로세스와 쓰레드

Transcription:

10 (GUI)

(Graphical User Interface) AWT/Swing Java java.awt, javax.swing

(Event-Driven Programming) :,,,, (event-handler or event-listener), Java (action event), (action listner).

Java AWT/Swing (component), (label), (text component), (button), (list) (container): (panel): (window), (frame): (dialog): (menu bar):,

(Layout) (layout manager) (flow): (border):, (grid):

AWT/Swing Component Container Window JApplet JComponen JFrame AbstractButt JLabel JList JMenuB JButton JMenuItem JMenu

,

JFrame. "Press This". "OK".....

import java.awt.*; import javax.swing.*; public class ButtonFrame extends JFrame { public ButtonFrame() { JLabel label = new JLabel("Press This:"); JButton button = new JButton("OK"); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(label); c.add(button); setsize(200, 60); setvisible(true); public static void main(string[] args) { new ButtonFrame();

: import java.awt.*; import javax.swing.*; public class ButtonFrame extends JFrame { public ButtonFrame() { JLabel label = new JLabel("Press this important button:"); JButton button = new JButton("PAV Lab", new ImageIcon("/pav-logo.gif")); Container c = getcontentpane(); c.setlayout(new FlowLayout()); label.setforeground(color.white); c.setbackground(new Color(100,100,100)); c.add(label); c.add(button); setsize(200, 160); setvisible(true); public static void main(string[] args) { new ButtonFrame();

. public interface ActionListener { public void actionperformed(actionevent e);.

, 1.

: public class Counter { private int count; public Counter(int start) { count = start; public void increment() { count++; public int countof() { return count;

import java.awt.*; import java.awt.event.*; import javax.swing.*; class Frame2a extends JFrame implements ActionListener { private Counter count; private JLabel label = new JLabel("count = 0"); public Frame2a(Counter c) { count = c; Container cp = getcontentpane(); cp.setlayout(new FlowLayout()); JButton button = new JButton("OK"); cp.add(label); cp.add(button); button.addactionlistener(this); // settitle("frame2a"); setsize(200, 60); setvisible(true); public void actionperformed(actionevent e) { count.increment(); label.settext("count = " + count.countof());

public class Example2a { public static void main(string[] args) { Counter model = new Counter(0); Frame2a view = new Frame2a(model);

: Example2a main(string[] args) Counter increment() countof() : int Frame2a Frame2a(Counter) actionperformed( ActionEvent) JLabel settext(string) JButton addactionlistener(ac tionlistener)

: Example2a main(string[] args) JLabel settext(string) Counter increment() countof() : int Frame2a Frame2a(Counter) actionperformed( ActionEvent) JButton addactionlistener(ac tionlistener)

: + JLabel settext(string) Counter increment() countof() : int Frame2a Frame2a(Counter) actionperformed( ActionEvent) JButton addactionlistener(ac tionlistener)

: # JLabel Counter increment() countof() : int Frame2a Frame2a(Counter) update() CountControll actionperformed( ActionEvent) settext(string) JButton addactionlistener(ac tionlistener)

import java.awt.*; import java.awt.event.*; import javax.swing.*; class Frame2b extends JFrame { private Counter count; private JLabel label = new JLabel("count = 0"); public Frame2b(Counter c) { count = c; Container cp = getcontentpane(); cp.setlayout(new FlowLayout()); JButton button = new JButton("OK"); cp.add(label); cp.add(button); button.addactionlistener(new CountController(count,this)); settitle("frame2b"); setsize(200, 60); setvisible(true); public void update() { // label.settext("count = " + count.countof());

import java.awt.event.*; public class CountController implements ActionListener { private Frame2b view; private Counter model; public CountController(Counter m, Frame2b v) { view = v; model = m; public void actionperformed(actionevent e) { model.increment(); view.update();

: + JLabel Counter increment() countof() : int Frame2a Frame2a(Counter) update() settext(string) CountButton addactionlistener(ac tionlistener) actionperformed(acti onevent)

import java.awt.*; import java.awt.event.*; import javax.swing.*; class Frame2c extends JFrame { private Counter count; private JLabel label = new JLabel("count = 0"); public Frame2b(Counter c) { count = c; Container cp = getcontentpane(); cp.setlayout(new FlowLayout()); CountButton button = new CountButton("OK", count, this); cp.add(label); cp.add(button); // settitle("frame2c"); setsize(200, 60); setvisible(true); public void update() { label.settext("count = " + count.countof());

+ import javax.swing.*; import java.awt.event.*; public class CountButton extends JButton implements ActionListener { private Frame2c view; private Counter model; public CountButton(String label, Counter m, Frame2c v) { super(label); view = v; model = m; addactionlistener(this); public void actionperformed(actionevent e) { model.increment(); view.update();

. 1 2 3.

,

import java.awt.event.*; import javax.swing.*; public class ExitButton extends JButton implements ActionListener { public ExitButton(String label) { super(label); addactionlistener(this); public void actionperformed(actionevent e) { System.exit(0);

import java.awt.*; import java.awt.event.*; import javax.swing.*; class Frame2c extends JFrame { private Counter count; private JLabel label = new JLabel("count = 0"); public Frame2b(Counter c) { count = c; Container cp = getcontentpane(); cp.setlayout(new FlowLayout()); cp.add(label); cp.add(new CountButton("OK",count,this)); cp.add(new ExitButton("Exit")); settitle("frame2c"); setsize(200, 60); setvisible(true); public void update() { label.settext("count = " + count.countof());

,...

(BorderLayout) Frame Panel p1 Panel Drawing Panel p2 p1 NORTH Frame, Drawing p2 SOUTH. Count, Drawing, Label Counter

Drawing import java.awt.*; import javax.swing.*; public class Drawing extends JPanel { private Counter count; public Drawing(Counter model) { count = model; setsize(200,80); public void paintcomponent(graphics g) { g.setcolor(color.white); // g.fillrect(0,0,200,80); g.setcolor(color.red); int x=0, y=0; for(int i=0; i<count.countof(); i++) { //. g.filloval(x*25, y*25, 20, 20); x++; if(x>7) { x=0; y++;

CountButton import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CountButton extends JButton implements ActionListener { private Frame4 view; private Counter model; public CountButton(String label, Counter m, Frame4 v) { super(label); view = v; model = m; addactionlistener(this); public void actionperformed(actionevent e) { model.increment(); view.update();

Frame import java.awt.*; import javax.swing.*; public class Frame4 extends JFrame { private Counter count; private JLabel lab = new JLabel("count = 0"); private JPanel drawing; public void update() { lab.settext("count = " + count.countof()); drawing.repaint();

Frame public Frame4(Counter c, JPanel panel) { count = c; drawing = panel; Container cp = getcontentpane(); cp.setlayout(new BorderLayout()); JPanel p1 = new JPanel(new FlowLayout()); JPanel p2 = new JPanel(new FlowLayout()); lab = new JLabel("count = " + count.countof()); p1.add(lab); p2.add(new CountButton("Count", count, this)); p2.add(new ExitButton("Quit")); cp.add(p1, BorderLayout.NORTH); cp.add(drawing); cp.add(p2, BorderLayout.SOUTH); settitle("example4"); setsize(200,150); setvisible(true);

public class Example4 { public static void main(string[] args) { Counter model = new Counter(0); Drawing drawing = new Drawing(model); Frame4 view = new Frame4(model, drawing);

,,. OK.

OK ColorButton ThrobPanel ThrobFrame ThrobController ThrobbingBall

ColorButton ColorButton (ThrobPanel f) actionperformed (ActionEvent e) ThrobPanel ThrobPanel (int size, ThrobbingBall b) getcolor() setcolor(color c) ThrobFrame ThrobFrame (int size, ThrobPanel p, ThrobControlle ThrobController (ThrobPanel w, ThrobbingBall b, int delay_time) run() ThrobbingBall islarge(): boolean throb()

ThrobbingBall public class ThrobbingBall { private boolean is_large = true; public boolean islarge() { return is_large; public void throb() { is_large = is_large;

ThrobPanel mport java.awt.*; import javax.swing.*; public class ThrobPanel extends JPanel { private int panel_size, location, ball_size; private Color c = Color.red; private ThrobbingBall ball; public ThrobPanel(int size, ThrobbingBall b) { panel_size = size; location = size/2; ball_size = size/3; ball = b; setsize(size, size); public Color getcolor() { return c; public void setcolor(color new_color) { c = new_color; public void paintcomponent(graphics g) { g.setcolor(color.white); g.fillrect(0, 0, panel_size, panel_size); g.setcolor(c); if (ball.islarge()) g.filloval(location-ball_size/2,location-ball_size/2,ball_size,ball_size); else g.filloval(location-ball_size/4,location-ball_size/4,ball_size/2,ball_size/2);

ColorButton import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ColorButton extends JButton implements ActionListener { private ThrobPanel view; public ColorButton(ThrobPanel f) { super("ok"); view = f; addactionlistener(this); public void actionperformed(actionevent e) { Color c = view.getcolor(); if(c==color.red) view.setcolor(color.blue); else view.setcolor(color.red);

ThrobFrame import java.awt.*; import javax.swing.*; public class ThrobFrame extends JFrame { public ThrobFrame(int size, ThrobPanel p, ColorButton b) { Container cp = getcontentpane(); cp.setlayout(new BorderLayout()); cp.add(p, BorderLayout.CENTER); cp.add(b, BorderLayout.SOUTH); settitle("throb"); setsize(size, size+40); setvisible(true);

ThrobController public class ThrobController { private ThrobPanel writer; private ThrobbingBall ball; private int time; public ThrobController(ThrobPanel w, ThrobbingBall b, int delay_time) { writer = w; ball = b; time = delay_time; public void run() { while(true) { ball.throb(); writer.repaint(); delay(); private void delay() { try { Thread.sleep(time); catch (InterruptedException e) {

public class StartThrob { public static void main(string[] a) { int frame_size = 180; int pause_time = 200; ThrobbingBall b = new ThrobbingBall(); ThrobPanel p = new ThrobPanel(frame_size, b); ThrobFrame f = new ThrobFrame(frame_size, p, new ColorButton(p)); new ThrobController(p, b, pause_time).run();

, SlidePuzzleBoard private PuzzlePiece[][] board SlidePuzzleBoard(int size) move(int w): boolean 1 * PuzzlePieces private int face_value PuzzlePiece(int value)

, PuzzleFrame PuzzleFrame(int board_size, SlidePuzzleBoard b) update() 1 * PuzzleButton actionperformed(action Event e) SlidePuzzleBoard private PuzzlePiece[][] board SlidePuzzleBoard(int size) move(int w): boolean 1 * PuzzlePieces private int face_value PuzzlePiece(int value)

PuzzleButton import javax.swing.*; import java.awt.event.*; public class PuzzleButton extends JButton implements ActionListener { private SlidePuzzleBoard puzzle; private PuzzleFrame view; public PuzzleButton(SlidePuzzleBoard p, PuzzleFrame v) { puzzle = p; view = v; addactionlistener(this); public void actionperformed(actionevent evt) { String s = gettext(); if(s.equals("")) { boolean ok = puzzle.move(new Integer(s).intValue()); if(ok) view.update();

PuzzleFrame public class PuzzleFrame extends JFrame { private SlidePuzzleBoard board; private int size, button_size = 60; private PuzzleButton[][] button; public PuzzleFrame(int board_size, SlidePuzzleBoard b) { size = board_size; board = b; button = new PuzzleButton[size][size]; Container cp = getcontentpane(); cp.setlayout(new GridLayout(size, size)); for (int i=0; i<size; i++) for(int j=0; j<size; j++) { button[i][j] = new PuzzleButton(board, this); cp.add(button[i][j]); update(); settitle("puzzleframe"); setsize(size * button_size + 10, size * button_size + 20); setvisible(true);

PuzzleFrame public void update() { PuzzlePiece[][] r = board.contents(); for(int i=0; i<size; i++) for(int j=0; j<size; j++) { if (r[i][j]=null) { button[i][j].setbackground(color.white); button[i][j].settext("" + r[i][j].valueof()); else { button[i][j].setbackground(color.black); button[i][j].settext("");

, (Scrolling List)

ListFrame ListFrame(Counter2[] model) getselecton(): int update() Counter2 private PuzzlePiece[][] board SlidePuzzleBoard(int size) move(int w): boolean JList ListButton ListButton(String label, Counter2[] c, ListFrame v) actionperformed(actionevent

Counter2 public class Counter2 { private int count, my_index; public Counter2(int start, int index) { count = start; my_index = index; public void increment() { count++; public int countof() { return count; public String tostring() { return "Counter " + my_index + " has " + countof();

ListButton import java.awt.event.*; import javax.swing.*; public class ListButton extends JButton implements ActionListener { private Counter2[] counters; private ListFrame view; public ListButton(String label, Counter2[] c, ListFrame v) { super(label); counters = c; view = v; addactionlistener(this); public void actionperformed(actionevent evt) { int choice = view.getselection(); if(choice = -1) { counters[choice].increment(); view.update();

ListFrame import java.awt.*; import javax.swing.*; public class ListFrame extends JFrame { private Counter2[] counters; private JList items; public ListFrame(Counter2[] model) { counters = model; items = new JList(counters); JScrollPane sp = new JScrollPane(items); JPanel p = new JPanel(new GridLayout(2,1)); p.add(new ListButton("Go", counters, this)); p.add(new ExitButton("Quit")); Container cp = getcontentpane(); cp.setlayout(new GridLayout(2,1)); cp.add(sp); cp.add(p); update(); settitle("listexample"); setsize(200,200); setvisible(true); public int getselection() { return items.getselectedindex(); public void update() { items.clearselection();

JList. items.addlistselectionlistener(listselectionlisten er); valuechanged.. items.setselectionmode(listselectionmodel.multi PLE_INTERVAL_SELECTION). items.getselectedindices(): int[]

JTextField input_text = new JTextField(, ) input_text.gettext(): input_text.settext(... ):

JTextArea JTextField JTextArea text = new JTextArea(, 20, 40); text.setlinewrap(true); text.setfont(new Font( Courier, Font.PLAIN, 14)); JScrollPane sp = new JScrollPane(text);

JTextArea JTextComponent gettext(): String, settext(string) getcaretposition(): int, setcaretposition(int), movecaretposition(int) getselectedtext(): String, getselectionstart(): int, getselectionend(): int cut(), copy(), paste() iseditable(): boolean, seteditable(boolean) JTextArea setfont(font) setlinewrap(boolean) insert(string, int) replacerange(string, int, int)

JMenu & JMenuBar JMenuBar mbar = new JMenuBar(); JMenu file = new JMenu( File ); mbar.add(file);. JMenu edit = new JMenu( Edit ); edit.add(new JMenuItem( Cut )); edit.add(new JMenuItem( Copy )); edit.add(new JMenuItem( Paste )); edit.addseparator(); JMenu search = new JMenu( Search );... edit.add(search); mbar.add(edit); setjmenubar(mbar);

,

,

,

ActionListener AWT/Swing classes that detect events ClearMenuItem QuitMenuItem CutMenuItem CopyMenuItem PasteMenuItem FindMenuItem ReplaceMenuItem EditModel EditFrame ReplaceFrame JTextArea JFrame

EditModel import java.awt.*; import javax.swing.*; public class EditModel extends JTextArea { public EditModel(String initial_text, int rows, int cols) { super(initial_text, rows, cols); setlinewrap(true); setfont(new Font("Courier", Font.PLAIN, 14)); public void clear() { settext(""); private int find(string s, int position) { int index = gettext().indexof(s, position); if(index = -1) { setcaretposition(index + s.length()); movecaretposition(index); return index; public int findfromstart(string s) { return find(s, 0); public int findfromcaret(string s) { return find(s, getcaretposition());

QuitMenuItem import javax.swing.*; import java.awt.event.*; public class QuitMenuItem extends JMenuItem implements ActionListener { public QuitMenuItem(String label) { super(label); addactionlistener(this); public void actionperformed(actionevent e) { System.exit(0);

: EditorMenuItem import javax.swing.*; import java.awt.event.*; public abstract class EditorMenuItem extends JMenuItem implements ActionListener { private EditModel buffer; public EditorMenuItem(String label, EditModel model) { super(label); buffer = model; addactionlistener(this); public EditModel mymodel() { return buffer; public abstract void actionperformed(actionevent e);

ClearMenuItem import java.awt.event.*; public class ClearMenuItem extends EditorMenuItem { public ClearMenuItem(String label, EditModel model) { super(label, model); public void actionperformed(actionevent e) { mymodel().clear(); cut/copy/pastemenuitem

EditFrame import java.awt.*; import javax.swing.*; public class EditFrame extends JFrame { private EditModel buffer = new EditModel("", 15, 50); public EditFrame() { // ReplaceFrame second_frame = new ReplaceFrame(buffer); Container cp = getcontentpane(); cp.setlayout(new BorderLayout()); JMenuBar mbar = new JMenuBar(); JMenu file = new JMenu("File"); file.add(new ClearMenuItem("New", buffer)); file.add(new QuitMenuItem("Exit")); mbar.add(file); JMenu edit = new JMenu("Edit"); edit.add(new CutMenuItem("Cut", buffer)); edit.add(new CopyMenuItem("Copy", buffer)); edit.add(new PasteMenuItem("Paste", buffer)); edit.addseparator();

ClearMenuItem // JMenu search = new JMenu("Search"); // search.add(new FindMenuItem("Find", buffer)); // search.add(new ReplaceMenuItem("Replace", // second_frame)); // edit.add(search); mbar.add(edit); setjmenubar(mbar); JScrollPane sp = new JScrollPane(buffer); cp.add(sp, BorderLayout.CENTER); settitle("editframe"); pack(); setvisible(true); public static void main(string[] args) { new EditFrame();

FindMenuItem import java.awt.event.*; import javax.swing.*; public class FindMenuItem extends EditorMenuItem { public FindMenuItem(String label, EditModel model) { super(label, model); public void actionperformed(actionevent e) { String s = JOptionPane.showInputDialog(this, "Type string to be found:"); if(s = null) { if(mymodel().findfromcaret(s) == -1) { int response = JOptionPane.showConfirmDialog(this, "String " + s + " not found. Restart search from beginning of buffer?"); if(response == JOptionPane.YES_OPTION) { if(mymodel().findfromstart(s) == -1) JOptionPane.showMessageDialog(this, "String " + s + " not found.");

ReplaceMenuItem import java.awt.event.*; import javax.swing.*; public class ReplaceMenuItem extends JMenuItem implements ActionListener { private ReplaceFrame view; public ReplaceMenuItem(String label, ReplaceFrame v) { super(label); view = v; addactionlistener(this); public void actionperformed(actionevent e) { view.setvisible(true);

ReplaceFrame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ReplaceFrame extends JFrame implements ActionListener { private EditModel model; private JButton replace = new JButton("Replace"); private JButton clear = new JButton("Clear"); private JButton close = new JButton("Close"); private JTextField find_text = new JTextField("", 20); private JTextField replace_text = new JTextField("", 20); public ReplaceFrame(EditModel m) { model = m; Container cp = getcontentpane(); cp.setlayout(new BorderLayout()); JPanel p1 = new JPanel(new GridLayout(2, 1)); JPanel p11 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); p11.add(new JLabel("From caret, replace ")); p11.add(find_text); p1.add(p11);

ReplaceFrame JPanel p12 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); p12.add(new JLabel("by ")); p12.add(replace_text); p1.add(p12); cp.add(p1, BorderLayout.CENTER); JPanel p2 = new JPanel(new FlowLayout()); p2.add(replace); p2.add(clear); p2.add(close); cp.add(p2, BorderLayout.SOUTH); replace.addactionlistener(this); clear.addactionlistener(this); close.addactionlistener(this); settitle("replaceframe"); pack(); setvisible(false);

ReplaceFrame public void actionperformed(actionevent e) { if(e.getsource() == close) { setvisible(false); else if(e.getsource() == clear) { find_text.settext(""); replace_text.settext(""); else if(e.getsource() == replace) { String find = find_text.gettext(); int location = model.findfromcaret(find); if(location == -1) JOptionPane.showMessageDialog(this, "String " + find + " not found."); else model.replacerange(replace_text.gettext(), location, location+find.length());

. (listener object). b.addactionlistener(ob) actionperformed. GUI. Observable Observer(update ) addobserver.

implements Observer ➊ addobserver ➍ update implements Observable addobserver() setchanged() ➋ setchanged ➌ notifyobservers

,. public class Counter3 { private int count; public Counter3(int start) { count = start; public int countof() { return count; public void increment() { count++; System.out.println("new count = " + countof()); MVC

? public class Counter3 { private int count; private PrintCount view; public Counter3(int start, PrintCount v) { count = start; view = v; public int countof() { return count; public void increment() { count++; view.update(); public class PrintCount { private Counter3 counter; public PrintCount(Counter3 c) { counter = c; public void update() { System.out.println( "new count = " + counter.countof());

Observable, Observer public class Counter3 implements Observable { private int count; public Counter3(int start) { count = start; public int countof() { return count; public void increment() { count++; setchanged(); notifyobservers(); public class PrintCount implements Observer { private Counter3 counter; public PrintCount(Counter3 c){ counter = c; c.addobserver(this); public void update() { System.out.println( "new count = " + counter.countof()); : Counter3 PrintCount

, CountButton import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CountButton extends JButton implements ActionListener { private Frame4 view; private Counter model; public CountButton(String label, Counter m, Frame4 v) { super(label); view = v; model = m; addactionlistener(this); public void actionperformed(actionevent e) { model.increment(); view.update();

Observable, Observer import java.awt.*; import javax.swing.*; import java.awt.event.*; public class CountButton extends JButton implements ActionListener { private Frame4 view; private Counter3 model; public CountButton(String label, Counter3 m, Frame4 v) { super(label); view = v; model = m; addactionlistener(this); public void actionperformed(actionevent e) { model.increment(); view.update();

Frame import java.awt.*; import javax.swing.*; public class Frame4 extends JFrame implements Observer { private Counter count; private JLabel lab = new JLabel("count = 0"); private JPanel drawing; public void update() { lab.settext("count = " + count.countof()); drawing.repaint(); public Frame4(Counter c, JPanel panel) { c.addobserver(this);...

Observabl Counter Observer Frame4 Counter Frame4 CountButto CountButto