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