[ 10 ]
..,..,,,., 2
1. 3
Section 1 p408 (event) (listen) (event listener) 4
Section 1 [ 1: ] [ 2: ] 5
Section 1 (ActionEvent) (MouseEvent) 6
Section 1 EventObject getsource() 7
Section 1 8
Section 1 MouseListener 9
Section 1 java.awt.event EventListener 10
Section 1 11
2. 12
Section 2 p416 [ 1: ] ActionListener [ 2: ].addactionlistener( ) 13
ButtonActionWin Section 2 ActionListener actionperformed( ) btnok, btncancel addactionlistener( ) 14
10-1 Section 2 import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jbutton; import javax.swing.jlabel; import java.awt.borderlayout; import java.awt.gridlayout; public class ButtonActionWin extends JFrame implements ActionListener { private static final long serialversionuid = 1L; JButton btnok = new JButton("OK"); JButton btncancel = new JButton("Cancel"); JLabel dlb = new JLabel("."); public ButtonActionWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(300, 130); } // makebuttonandeventhandle(); setvisible(true); public void makebuttonandeventhandle() { JPanel p = new JPanel(new GridLayout(0, 2)); p.add(btnok); p.add(btncancel); add(p, BorderLayout.CENTER); add(dlb, BorderLayout.SOUTH); } //2 btnok.addactionlistener(this); btncancel.addactionlistener(this); public void actionperformed(actionevent evt) { // String strcmd = evt.getactioncommand(); } // if (strcmd.equals("ok")) { dlb.settext(" OK Button"); } else if (strcmd.equals("cancel")) { dlb.settext(" Cancel Button"); } public static void main(string[] args) { ButtonActionWin mywin = new ButtonActionWin(); mywin.settitle(" "); } } 15
Section 2 (press) (release) 16
10-2 Section 2 import java.awt.*; import java.awt.event.*; import javax.swing.jframe; public class MouseEventWin extends JFrame { private static final long serialversionuid = 1L; MouseEventWin childwin = null; static int countchild; public MouseEventWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(400, 150); setvisible(true); //register mouse event handler, motion event handler addmouselistener(new MyMouseListener()); } class MyMouseListener implements MouseListener { public void mousepressed(mouseevent e) { Rectangle bounds = getbounds(); int x = e.getx() + bounds.x; int y = e.gety() + bounds.y; 17
MouseListenr MouseEvent MouseListenr MouseEvent Section 2 18
MouseMotionListener Section 2 (move) (drag) MouseListener, MouseMotionListener 7 MouseInputListener 19
s1, s2, s3 Pressed/Released Entered/Exited/ Clicked Dragged/Moved Section 2 20
7 MyMouseListener Section 2 MouseListener 5 MouseMotionListener 2 21
4 4 [ 1: ] [ 2: ] [ 3: ] [ 4: ] Section 2 22
Section 2 4 ActionEvent 23
Section 2 24
Section 2 25
3. 26
Section 3 p432 MouseListener MouseMotionListener 5 2 7 3 27
Section 3 28
Section 3 MouseAdapter MouseMotionAdapter mousemoved() 29
4. 30
Section 4 p110,,, JMenuItem JCheckBoxMenuItem ComponentEvent ComponentEvent getcomponent() 31
10-5 Section 4 import java.awt.event.componentadapter; import java.awt.event.componentevent; import javax.swing.jframe; import javax.swing.jtextarea; public class ComponentEventWin extends JFrame { private static final long serialversionuid = 1L; JTextArea txtarea = new JTextArea(); class MyComponentAdapter extends ComponentAdapter { public void componentresized(componentevent evt) { String str = evt.getsource().getclass() + " : "; str += evt.getcomponent().getbounds() + "\n"; txtarea.append(str); } } public ComponentEventWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(600, 150); add(txtarea, "Center"); txtarea.addcomponentlistener(new MyComponentAdapter()); setvisible(true); } public static void main(string[] args) { ComponentEventWin mywin = new ComponentEventWin(); mywin.settitle(" "); } } 32
Section 4 ItemEvent ItemEvent JCheckBox, JList, JComboBox, JCheckBox, MenuItem ItemEvent ItemSelectable on/off selected/deselected ItemListener ItemEvent 33
JCheckBoxMenuItem JMenu JCheckBoxMenuItem mainmenu.add(submenu) JCheckBoxMenuItem Section 4 / additemlistener( ) itemstatechanged() 34
Section 4 10-6 JCheckBox JCheckBoxMenuItem ItemEvent JCheckBoxMenuItem JCheckBox JList JLable ItemEvent 35
10-6 import java.awt.event.itemevent; import java.awt.event.itemlistener; import javax.swing.abstractbutton; import javax.swing.jcheckbox; import javax.swing.jcheckboxmenuitem; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jpanel; import javax.swing.jtextarea; public class ItemEventWin extends JFrame implements ItemListener { private static final long serialversionuid = 1L; JPanel pcheck = new JPanel(); // JTextArea txtarea = new JTextArea(); JLabel status = new JLabel(" "); public ItemEventWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(300, 200); } makecheckbox(); makemenuandeventhandle(); add(pcheck, "North"); add(txtarea, "Center"); add(status, "South"); setvisible(true); public void makecheckbox() { JCheckBox box1 = new JCheckBox(" "); JCheckBox box2 = new JCheckBox(" "); JCheckBox box3 = new JCheckBox(" "); JCheckBox box4 = new JCheckBox(" "); box1.additemlistener(this); box2.additemlistener(this); 36 } Section 4 box3.additemlistener(this); box4.additemlistener(this); pcheck.add(box1); pcheck.add(box2); pcheck.add(box3); pcheck.add(box4); } public void makemenuandeventhandle() { JMenuBar mbar = new JMenuBar(); JMenu mainmenu = new JMenu(" "); JMenu submenu = new JMenu(" "); JCheckBoxMenuItem subcheck1 = new JCheckBoxMenuItem(" "); JCheckBoxMenuItem subcheck2 = new JCheckBoxMenuItem(" "); submenu.add(subcheck1); submenu.add(subcheck2); subcheck1.additemlistener(this); subcheck2.additemlistener(this); } mainmenu.add(submenu); mbar.add(mainmenu); setjmenubar(mbar); public void itemstatechanged(itemevent evt) { String str = ((AbstractButton) evt.getitem()).gettext(); if (evt.getstatechange() == ItemEvent.DESELECTED) str += " \n"; else str += " \n"; status.settext(str); txtarea.append(str); } public static void main(string[] args) { ItemEventWin mywin = new ItemEventWin(); mywin.settitle(" "); }
Section 4 Container, JDialog, JFrame, JPanel, Jwindow ContainerListener ContainerListener ContainerEvent 37
Section 4,, Vector, [Add button] [Remove button] [Add button] [Remove button] 38
10-7 import java.awt.borderlayout; import java.awt.gridlayout; Section 4 import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.containerevent; import java.awt.event.containerlistener; import javax.swing.*; import java.util.vector; public class ContainerEventWin extends JFrame implements ContainerListener, ActionListener { private static final long serialversionuid = 1L; JButton btnadd = new JButton("Add button"); JButton btnremove = new JButton("Remove button"); JLabel lbl = new JLabel(); JPanel p1 = new JPanel(); JButton recentbtn; // int btncount = 0; Vector<JButton> arybtn = new Vector<JButton>(); public ContainerEventWin() { JPanel p2 = new JPanel(new GridLayout(0, 2)); p2.add(btnadd); p2.add(btnremove); add(lbl, BorderLayout.NORTH); add(p1, BorderLayout.CENTER); add(p2, BorderLayout.SOUTH); // p1.addcontainerlistener(this); // btnadd.addactionlistener(this); btnremove.addactionlistener(this); } setsize(300, 200); setvisible(true); //ActionListener's method implementation 39
Section 4 Component FocusListener FocusAdapter FocusListener 40
Section 4 10-8 import java.awt.event.focusevent; import java.awt.event.focuslistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextarea; public class FocusEventWin extends JFrame implements FocusListener { private static final long serialversionuid = 1L; JTextArea txtarea = new JTextArea(); public FocusEventWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(350, 150); // makewindowandeventhandle(); setvisible(true); } public void makewindowandeventhandle() { JButton btn = new JButton(" "); add(txtarea, "Center"); add(btn, "South"); // 41
5. 42
Section 5 p448 (Graphics Context) 43
Section 5 Graphics Graphics,,, paint() update() paintcomponent() 44
Section 5 Color (graphics context) (foreground) Graphics setcolor() Component setbackground() setforeground() Color (background) RGB 45
Section 5 10-9 paint() Graphics g setcolor() drawstring(, x, y) package graphics; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import javax.swing.jframe; public class DrawStringWin extends JFrame { private static final long serialversionuid = 1L; public DrawStringWin() { setdefaultcloseoperation(jframe.exit_on_close); setsize(300, 150); setvisible(true); } public 10 void paint(graphics g) { 46
Section 5 drawroundrect() arcwidth argheight drawrect(), fillroundrect() 47
Section 5 10-10 13 4 13 switch-case, 48
10-10 Section 5 package graphics; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.graphics; import javax.swing.jframe; public class DrawLineWin extends JFrame { private static final long serialversionuid = 1L; public DrawLineWin() { setdefaultcloseoperation(jframe.exit_on_close); } setsize(400, 200); setvisible(true); public void paint(graphics g) { //get ContentPane Container win = getcontentpane(); // Dimension d = win.getsize(); int clientwidth = d.width; // int clientheight = d.height;// int incheight = (clientheight-10) / 12; for (int i = 0; i < 13; i++) { switch (i) { case 0: g.setcolor(color.black); break; case 1: g.setcolor(color.blue); break; case 2: g.setcolor(color.cyan); break; 10 case 3: g.setcolor(color.darkgray); 49
, Section 5 drawoval() drawarc() drawpolygon() fillpolygon() 50
Section 5 10-11 filloval() Color Runnable run() JFrame 51
10-11 Section 5 package graphics; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import javax.swing.jframe; public class DrawOvalWin extends JFrame implements Runnable { private static final long serialversionuid = 1L; public DrawOvalWin() { setdefaultcloseoperation(jframe.exit_on_close); new Thread(this).start(); } setsize(400, 200); setvisible(true); public void run() { while(true) { repaint(); try { Thread.sleep(200); } catch(exception e) { System.out.println(e); } } } 52
6. 53
Section 6 p460 drawbytes(), drawchars(), drawstring() (x, y) 54
Section 6 Font setfont() 55
Section 6 FontMetrics 56
Section 6 FontMetrics strwidth 57
10-12 Section 6 58
10-12 Section 6 package graphics; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphicsenvironment; import javax.swing.jframe; import javax.swing.jpanel; public class FontWin extends JFrame { private static final long serialversionuid = 1L; Color[] manycolors = { Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.yellow, Color.darkGray }; int[] styles = { Font.PLAIN, Font.ITALIC, Font.BOLD, Font.BOLD Font.ITALIC }; String[] stylenames = { "plain", "bold", "italic", "bold/ italic" }; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontnames = ge.getavailablefontfamilynames(); public FontWin() { setdefaultcloseoperation(jframe.exit_on_close); add(new MyPanel()); } setsize(400, 200); setvisible(true); private int random(int r) { return (int) Math.floor(Math.random() * r); } class MyPanel extends JPanel { private static final long serialversionuid = 1L; 59
Section 6 ImageIcon ImageIcon JLabel 60
Section 6 10-13 package graphics; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; public class ImageWin extends JFrame { private static final long serialversionuid = 1L; public ImageWin() { setdefaultcloseoperation(jframe.exit_on_close); ImageIcon img = new ImageIcon("image.png", " "); JLabel lb = new JLabel(img); add(lb); Integer height = new Integer(img.getIconHeight()); Integer width = new Integer(img.getIconWidth()); JLabel size = new JLabel(img.getDescription() + ": " + height + " " + width); add(size, "South"); setsize(400, 200); setvisible(true); } 10 61
Section 6 Graphics drawimage() drawimage(img, x, y, imgwidth, imgheight, this) Image img (x, y) Container getinsets() Insets in = getinsets(); Insets top, left, right, bottom 62
10-14 Section 6 package graphics; import java.awt.graphics; import java.awt.image; import java.awt.insets; import javax.swing.imageicon; import javax.swing.jframe; public class DrawImageWin extends JFrame { private static final long serialversionuid = 1L; Image img; Insets in; public DrawImageWin() { setdefaultcloseoperation(jframe.exit_on_close); ImageIcon imgico = new ImageIcon("image.png"); img = imgico.getimage(); setsize(350, 250); setvisible(true); in = getinsets(); System.out.println(in.top + " " + in.bottom + " " + in.left + " " + in.right); } public void paint(graphics g) { 10 //int origwidth = img.getwidth(this); 63
64
[ Add your company slogan ] Thank You!