I 4 p.1 4 GUI GUI GUI 4.1 4.1.1 MouseTest.java /* 1 */ public class MouseTest extends JApplet implements MouseListener /* 2 */ { int x=50, y=20; addmouselistener(this); /* 3 */ public void mouseclicked(mouseevent e) { /* 4 */ x = e.getx(); y = e.gety(); return; public void mousepressed(mouseevent e) { /* 5 */ public void mousereleased(mouseevent e) { /* 5 */ public void mouseentered(mouseevent e) { /* 5 */ public void mouseexited(mouseevent e) { /* 5 */ public void paint(graphics g) { super.paint(g); /* 6 */ g.drawstring("hello WORLD!", x, y);
I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mouseclicked /* 4 */ mouseclicked MouseEvent MouseListener mousepressed, mousereleased, mouseentered, mouseexited /* 5 */ paint super.paint(g) super. /* 6 */ x, y mouseclicked MouseEvent getx, gety repaint repaint JApplet paint 4.2 this this Java self Java object.methed(args1, args2,... ). object this 4.3 interface C++ Java
4.4. I 4 p.3 Java MouseListener public interface MouseListener { public void mouseclicked(mouseevent e); public void mousepressed(mouseevent e); public void mousereleased(mouseevent e); public void mouseentered(mouseevent e); public void mouseexited(mouseevent e); MouseListener MouseClicked addmouselistener MouseListener 4.3.1 repaint() MouseTest.java? 4.3.2 Othello.java 4.3.3 4.3.4 java.util.arraylist (100,100) 4.4 Mouse Key MouseTest.java 4.4.1 U(p), D(own)
I 4 p.4 4 GUI KeyTest.java public class KeyTest extends JApplet implements KeyListener { int x=50, y=20; addkeylistener(this); public void paint(graphics g) { super.paint(g); g.drawstring("hello WORLD!", x, y); public void keytyped(keyevent e) { int k = e.getkeychar(); if (k== u ) { y-=10; else if (k== d ) { y+=10; public void keyreleased(keyevent e) { public void keypressed(keyevent e) { KeyListner keypressed, keyreleased, keytyped 3 keypressed keyreleased KeyEvent getkeycode KeyTyped keypressed, keyreleased KeyEvent getkeychar ( Shift, a A ) 4.4.2 KeyTest.java KeyTest.java : (JDKDIR)/docs/ja/api/java.awt.event.KeyEvent.html 4.5 GUI GUI GUI
4.5. GUI I 4 p.5 ChangeColor.java Factorial.java mouseclicked keypressed GUI ActionEvent 4.5.1 ChangeColor.java public class ChangeColor extends JApplet implements ActionListener { Color[] cs = {Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE; int i=0; JButton b = new JButton("Next"); b.addactionlistener(this); /* 1 */ setlayout(new FlowLayout()); /* 2 */ add(b); /* 3 */ public void paint(graphics g) { super.paint(g); g.setcolor(cs[i]); g.drawstring("hello WORLD!", 20, 50); i=(i+1)%cs.length; JButton /* 3 */
I 4 p.6 4 GUI /* 2 */ add FlowLayout actionperformed b actionperformed b addactionlistener /* 1 */ GUI 1 actionperformed e actionperformed i GUI 2 4.5.2 Factorial.java public class Factorial extends JApplet implements ActionListener { JTextField input; JLabel output; input=new JTextField("0", 8); output=new JLabel(" 1"); input.addactionlistener(this); setlayout(new FlowLayout()); add(input); add(new JLabel(" ")); add(output); add(new JLabel(" ")); static int factorial(int n) { int r = 1; for (; n>0; n--) { r *= n; return r; // factorial int n = Integer.parseInt(input.getText()); output.settext(" "+factorial(n)); JTextField String int 2 JLabel GUI actionperformed actionperformed input JTextField gettext output JLabel settext
4.6. GUI I 4 p.7 4.6 GUI 4.6.1 2 UpDownButton.java public class UpDownButton extends JApplet implements ActionListener { int x=20; JButton left, right; left = new JButton("Left"); right = new JButton("Right"); left.addactionlistener(this); right.addactionlistener(this); setlayout(new FlowLayout()); add(left); add(right); public void paint(graphics g) { super.paint(g); g.drawstring("hello WORLD!", x, 55); if (e.getsource() == left) { // Left x-=10; else if (e.getsource() == right) { // Right x+=10; GUI 2 actionperformed ActionEvent getsource == 4.6.2 Factorial.java 2 = 9 + 32 5 0 32 100 212 String double double String 3
I 4 p.8 4 GUI 4.7 GUI if else getsource innner class 4.7.1 UpDownButton.java UpDownButton2.java public class UpDownButton2 extends JApplet { int x=20; public class LeftListener implements ActionListener { x-=10; public class RightListener implements ActionListener { x+=10; JButton left = new JButton("Left"); JButton right = new JButton("Right"); left.addactionlistener(new LeftListener()); right.addactionlistener(new RightListener()); setlayout(new FlowLayout()); add(left); add(right); public void paint(graphics g) { super.paint(g); g.drawstring("hello WORLD!", x, 55); Java C LeftListener RightListener ActionPerformed x repaint addactionlistner getsource
4.8. I 4 p.9 4.8, new ( ) { ActionListener java.lang.object 4.8.1 UpDownButton.java (anonymous class) UpDownButton3.java public class UpDownButton3 extends JApplet { int x=20; JButton left = new JButton("Left"); JButton right = new JButton("Right"); left.addactionlistener(new ActionListener() { x-=10; ); right.addactionlistener(new ActionListener() { x+=10; ); setlayout(new FlowLayout()); add(left); add(right); public void paint(graphics g) { super.paint(g); g.drawstring("hello WORLD!", x, 55);
I 4 p.10 4 GUI 4.9 JTextArea 4.9.1 JTextArea Divisor.java public class Divisor extends JApplet implements ActionListener { JTextField input; JTextArea output; input = new JTextField("0", 8); output = new JTextArea(10, 20); input.addactionlistener(this); setlayout(new FlowLayout()); add(input); add(output); int i, n = Integer.parseInt(input.getText()); for(i=1; i<=n; i++) { if (n%i==0) { output.append(i+" "+n+" Y=n"); output.append(" Y=nY=n"); JTextArea
4.9. JTextArea I 4 p.11 4.9.2 JPanel, JCheckBox, JComboBox, JList, JTable, JTree GUI 4.9.3 FlowLayout GUI Layout keytyped, mouseclicked, actionperformed, interface, MouseListener, KeyListener, ActionListener, this, MouseEvent, KeyEvent, ActionEvent, add, JButton, JLabel, JTextField, JTextArea
I 4 p.12 4 GUI :