home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 June / Image.iso / xtension / jbeans / textbean.txt next >
Encoding:
Text File  |  1997-04-07  |  1.6 KB  |  76 lines

  1. // BlinkText.java
  2. // Einfaches Java Bean, das einen Text darstellt
  3.  
  4. package demo.beans;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. public class TextBean extends Canvas implements MouseListener {
  10.  
  11.   // Eigenschaften
  12.   ActionListener actionlistener = null;
  13.   protected String text = "Hallo!";
  14.  
  15.   // Initialisierung
  16.   public TextBean() {
  17.     setSize(100, 100);
  18.     addMouseListener(this);
  19.   }
  20.  
  21.   // Schnittstellenfunktionen
  22.   public void setText(String t) {
  23.     text = t;
  24.   }
  25.   
  26.   public String getText() {
  27.     return text;
  28.   }
  29.   
  30.   // Ausgabe des Texts
  31.   public void paint(Graphics g) {
  32.     g.drawString(text, 10, 20);
  33.   }
  34.  
  35.   // Austauschen von Vordergrund- und Hintergrundfarbe
  36.   public void swapColors() {
  37.     Color c = getBackground();
  38.     setBackground(getForeground());
  39.     setForeground(c);
  40.   }
  41.  
  42.   // Registrierung von Listenern
  43.   public void addActionListener(ActionListener listener) throws java.util.TooManyListenersException {
  44.     if (actionlistener == null)
  45.       actionlistener = listener;
  46.     else
  47.       throw new java.util.TooManyListenersException();
  48.   }
  49.  
  50.   public void removeActionListener(ActionListener listener) {
  51.     if (actionlistener == listener)
  52.       actionlistener = null;
  53.   }
  54.  
  55.   // Ereignisbearbeitung
  56.   public void mouseClicked(MouseEvent e) {
  57.     if (actionlistener != null)
  58.       actionlistener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Click!"));
  59.   }
  60.  
  61.   public void mouseEntered(MouseEvent e) {
  62.   }
  63.  
  64.   public void mouseExited(MouseEvent e) {
  65.   }
  66.  
  67.   public void mousePressed(MouseEvent e) {
  68.   }
  69.  
  70.   public void mouseReleased(MouseEvent e) {
  71.   }
  72. }
  73.  
  74.  
  75.  
  76.