summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing2905.java
blob: 1a8d40c5e462f041f1a364c51b2ee6cfe7adde10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Listing2905.java */

import java.awt.*;
import java.awt.event.*;

public class Listing2905
extends Frame
implements FocusListener
{
  boolean havefocus = false;

  public static void main(String[] args)
  {
    Listing2905 wnd = new Listing2905();
  }

  public Listing2905()
  {
    super("Focus-Listener");
    addFocusListener(this);
    addWindowListener(new WindowClosingAdapter(true));
    setBackground(Color.lightGray);
    setSize(300,200);
    setLocation(200,100);
    setVisible(true);
  }

  public void paint(Graphics g)
  {
    if (havefocus) {
      g.setColor(Color.black);
      g.drawString("Fokus erhalten",10,50);
    } else {
      g.setColor(Color.darkGray);
      g.drawString("Kein Fokus",10,50);
    }
  }

  public void focusGained(FocusEvent event)
  {
    havefocus = true;
    setBackground(Color.yellow);
    repaint();
  }

  public void focusLost(FocusEvent event)
  {
    havefocus = false;
    setBackground(Color.lightGray);
    repaint();
  }
}