The next program example is a little more complicated, though it's of little value. The program is a small circle that is drawn around it regardless of when the mouse moves, and an action receiver is activated. Canvas. When you press the mouse button, we can change the property is the size of the circle, in addition to the text is displayed color, size, content. Bangbean also has its own addactionlistener () and Removeactionlistener () method, so we can attach our own receiver that will be activated when the user clicks on the Bangbean. In this way, we will be able to confirm the properties and events that can be supported:
: Bangbean.java
A Graphical Bean
Package Bangbean;
Import java.awt.*;
Import java.awt.event.*;
Import java.io.*;
Import java.util.*;
public class Bangbean extends Canvas
Implements Serializable {
protected int xm, YM;
protected int csize = 20; Circle size
protected String Text = "bang!";
protected int fontsize = 48;
protected Color TColor = color.red;
protected ActionListener ActionListener;
Public Bangbean () {
Addmouselistener (New ML ());
Addmousemotionlistener (New MML ());
}
public int getcirclesize () {return csize;}
public void setcirclesize (int newsize) {
CSize = newsize;
}
Public String Getbangtext () {return text;}
public void Setbangtext (String newtext) {
Text = NewText;
}
public int getfontsize () {return fontsize;}
public void setfontsize (int newsize) {
FontSize = newsize;
}
Public Color GetTextColor () {return tcolor;}
public void SetTextColor (Color newcolor) {
TColor = Newcolor;
}
public void Paint (Graphics g) {
G.setcolor (Color.Black);
G.drawoval (XM-CSIZE/2, YM-CSIZE/2,
CSize, CSize);
}
This is a unicast listener and which is
The simplest form of listener management:
public void addActionListener (
ActionListener l)
Throws Toomanylistenersexception {
if (ActionListener!= null)
throw new Toomanylistenersexception ();
ActionListener = l;
}
public void Removeactionlistener (
ActionListener l) {
ActionListener = null;
}
Class ML extends Mouseadapter {
public void mousepressed (MouseEvent e) {
Graphics g = getgraphics ();
G.setcolor (TColor);
G.setfont (
New Font (
"Timesroman", Font.Bold, FontSize));
int width =
G.getfontmetrics (). Stringwidth (text);
g.DrawString (Text,
(GetSize (). Width-width)/2,
GetSize (). HEIGHT/2);
G.dispose ();
Call the Listener ' s method:
if (ActionListener!= null)
Actionlistener.actionperformed (
New ActionEvent (Bangbean.this,
Actionevent.action_performed, null));
}
}
Class MML extends Mousemotionadapter {
public void mousemoved (MouseEvent e) {
XM = E.getx ();
ym = E.gety ();
Repaint ();
}
}
Public Dimension getPreferredSize () {
return new Dimension (200, 200);
}
Testing the Bangbean:
public static void Main (string[] args) {
Bangbean BB = new Bangbean ();
try {
Bb.addactionlistener (New BBL ());
catch (Toomanylistenersexception e) {}
Frame aframe = new Frame ("Bangbean Test");
Aframe.addwindowlistener (
New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
Aframe.add (BB, borderlayout.center);
Aframe.setsize (300,300);
Aframe.setvisible (TRUE);
}
During testing, send action information
To the console:
Static Class BBL implements ActionListener {
public void actionperformed (ActionEvent e) {
System.out.println ("Bangbean action");
}
}
} ///:~
Most importantly, we will notice that Bangbean performs this concatenation of interfaces. This means that the Application builder can store all the information in a concatenation for Bangbean after the program designer has adjusted the property values. When the bean is created as part of the running application, those stored properties are restored, so we can get our design right.
We can see that all of the fields that are normally run with the bean are private-allow access only through methods, typically using the properties structure.
When we look at the signature of addActionListener (), we notice that it can produce a toomanylistenerexception (too many receiver exceptions). This exception indicates that it is a single type, meaning that when an event occurs, it can only notify one receiver. In general, we use events with multiple types so that one event notifies multiple receivers. However, that will fall into the end of the next chapter that we are ready for, so the content will be recalled (the next heading is "The Java Beans Review"). A single type of event begs the question.
When we press the mouse button, the text is placed in the middle of the Bangbean, and if the Action Receiver field exists, its actionperformed () method is invoked to create a new ActionEvent object in the process. Whenever the mouse moves, its new coordinates will be captured, and the canvas will be repaint (as we have seen erase some of the text on the canvas).
The main () method increases the functionality that allows us to test the program from the command line. When a bean is in a development environment, the main () method is not used, but owning it is absolutely beneficial because it provides a quick test capability. Whenever a actionevent occurs, the main () method creates a frame and places a bangbean inside it, along with a simple action receiver attached to the Bangbean to print to the console. Of course, the application Builder typically creates the code for most beans. When we run Bangbean through Beandumper or by placing bangbean into an active bean development environment, we notice that there are a lot of additional attributes and actions that are significantly larger than the above code. That's because Bangbean inherits from the canvas, and the canvas is a bean, so we see its properties and events as appropriate.