|
This is a pretty simple applet. Its function is to provide a label and a control
to alter the font size of the text in the label. Despite its simplicity, this
applet demonstrates a number of principles.
Layout
This applet uses the simple XYLayout. This is not a very flexible layout manager,
but for the purpose of this applet it is adequate.
Controls
This applet has a JBCL TextControl with a position of 0,0 and a height and
width determined by the contents (-1,-1). It also has a jclass.bwt.JCSpinBox
which controls the size of the label font.
Source
package VariableSizeFont;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import jclass.bwt.*;
import com.sun.java.swing.*;
public class UIApplet extends Applet
{
XYLayout appletLayout = new XYLayout(320,200);
boolean isStandalone = false;
TextControl label = new TextControl();
jclass.bwt.JCSpinBox labelFontSizeSpinBox = new
jclass.bwt.JCSpinBox();
TextControl instructions = new TextControl();
public String getParameter(String key, String def)
{
return isStandalone ? System.getProperty(key,
def) :
(getParameter(key) != null ? getParameter(key)
: def);
}
public UIApplet()
{
}
public void init()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
instructions.setText("Change font
size with arrows");
label.setBackground(SystemColor.text);
label.setForeground(SystemColor.textText);
label.setAlignment(borland.jbcl.util.Alignment.CENTER
| borland.jbcl.util.Alignment.TOP);
label.setFont(new Font("TimesRoman",
0, 8));
label.setText("Label");
labelFontSizeSpinBox.setBackground(Color.lightGray);
labelFontSizeSpinBox.setFont(new
Font("TimesRoman", 0, 24));
labelFontSizeSpinBox.setAlignment(jclass.bwt.BWTEnum.CENTER);
labelFontSizeSpinBox.setEditable(false);
labelFontSizeSpinBox.setMinimum(8);
labelFontSizeSpinBox.setText("8");
labelFontSizeSpinBox.addSpinBoxListener(new
jclass.bwt.JCSpinBoxAdapter()
{
public void
spinBoxChangeEnd(JCSpinBoxEvent e)
{
labelFontSizeSpinBox_spinBoxChangeEnd(e);
}
});
this.setLayout(appletLayout);
this.add(label, new XYConstraints(0,
0, -1, -1));
this.add(labelFontSizeSpinBox,
new XYConstraints(9, 146, 103, -1));
this.add(instructions, new XYConstraints(130,
154, -1, -1));
}
public String getAppletInfo()
{
return "Applet Information";
}
public String[][] getParameterInfo()
{
return null;
}
void labelFontSizeSpinBox_spinBoxChangeEnd(JCSpinBoxEvent
e)
{
Font font = new Font
(
this.label.getFont().getName(),
this.label.getFont().getStyle(),
Integer.parseInt(this.labelFontSizeSpinBox.getText())
);
this.label.setFont(font);
this.label.setSize(this.label.getPreferredSize());
this.repaint();
}
}
|
Comments
There are several things which I found unusual in this program, given my C++
Builder experience.
- The complex structure required to manage events. A C++ Builder program
has a method declared in the code. The .dfm associates the method with the
event. The rest is handled without programming. Java, even under JBuilder,
still needs to follow the more complex Java way of handling events.
- The need to force a control to resize and repaint itself when a property
changes value. Most C++ Builder controls know enough to do that without
programming. Note that the label did not resize itself when its font size
changed; it was necessary to request the preferred size of the label after
the change, and to set its size accordingly. This may suggest a fruitful area
for developing a new bean, but as of the date of writing (9/14/98), I have
not yet explored that.
|
|