要求选择List框中的字体选项,改变Button按钮的字体,程序如下:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class test extends Applet implements ItemListener
{
Checkbox bg,cb1,cb2,cb3;
CheckboxGroup style;
Choice size;
List lt;
Button btn;
public void init()
{
style= new CheckboxGroup();
cb1 = new Checkbox("ÆÕͨ",true,style);
cb2 = new Checkbox("ºÚÌå",true,style);
cb3 = new Checkbox("бÌå",true,style);
bg= new Checkbox("±³¾°É«");
lt= new List(3,false);
lt.add("TimesRoman");
lt.add("SansSerif");
lt.add("Courier");
size=new Choice();
size.addItem("10");
size.addItem("12");
size.addItem("14");
btn=new Button("Ч¹û");
add(lt);
add(cb1);
add(cb2);
add(cb3);
add(size);
add(bg);
add(btn);
size.addItemListener(this);
lt.addItemListener(this);
bg.addItemListener(this);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
Checkbox cbtemp;
Choice ctemp;
List ltemp;
Font oldF=btn.getFont();
String s;
int si;
if(e.getItemSelectable()instanceof Checkbox)
{
cbtemp=(Checkbox)(e.getItemSelectable());
if(cbtemp.getLabel()=="±³¾°É«")
if(cbtemp.getState())
btn.setBackground(Color.gray);
else
btn.setBackground(Color.cyan);
if(cbtemp.getLabel()=="ÆÕͨ")
btn.setFont(new Font(oldF.getName(),Font.PLAIN,oldF.getSize()));
if(cbtemp.getLabel()=="ºÚÌå")
btn.setFont(new Font(oldF.getName(),Font.BOLD,oldF.getSize()));
if(cbtemp.getLabel()=="бÌå")
btn.setFont(new Font(oldF.getName(),Font.ITALIC,oldF.getSize()));
}
if(e.getItemSelectable()instanceof Choice)
{
ctemp=(Choice)(e.getItemSelectable());
s=ctemp.getSelectedItem();
si=Integer.parseInt(s);
btn.setFont(new Font(oldF.getName(),oldF.getStyle(),si));
}
if(e.getItemSelectable()instanceof List)
{
ltemp=(List)(e.getItemSelectable());
btn.setFont(new Font(ltemp.getSelectedItem(),oldF.getStyle(),oldF.getSize()));
}
}
}
其htm文件如下:
<html>
<head><title>Test</title></head>
<body>
<hr>
<applet code=test.class Width=600 height=400>
</applet>
</body>
</html>
请各位高手指点一下!