代码如下
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;public class Mybuttons extends Applet implements ActionListener, ItemListener{
private Button b1,b2,b3;

private TextField t1,t2,t3;
private String password;

private Choice c1;
private Font f1;
private TextField t4;

public void init(){
password="1234";
b1=new Button("Click Here!");
b2=new Button("Sorry,I don't do anything");
b3=new Button();

t1=new TextField("Enter PassWord!");
t1.setEditable(false);

t2=new TextField(12);
t2.setEchoChar('*');

t3=new TextField(30);
t3.setEditable(false);

c1=new Choice();
c1.addItem("TimesRoman");
c1.addItem("Courier");
c1.addItem("Helvetica");
t4=new TextField("Sample Text",16);
t4.setEditable(false);
f1=new Font(c1.getItem(1),Font.PLAIN,14);
t4.setFont(f1);


add(b1);
add(b2);
add(b3);

add(t1);
add(t2);
add(t3);

add(c1);
add(t4);

b1.addActionListener(this);
b3.addActionListener(this);

t2.addActionListener(this);

c1.addItemListener(this);
}
public void itemStateChanged(ItemEvent e){

if(e.getStateChange()==ItemEvent.SELECTED){
        
  f1=new Font(c1.getSelectedItem(),Font.PLAIN,14);
  t4.setFont(f1);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
showStatus("You had clicked b1!");
else if(e.getSource()==b3)
showStatus("You had clicked b3!");
else if(e.getSource()==t2){
if(t2.getText().equals(password))
t3.setText("Access Granted!");
else
t3.setText("Wrong Password!"+"Access Denied!");
}

 
  }
 


}

解决方案 »

  1.   

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;public class Mybuttons extends Applet implements ActionListener, ItemListener{
    private Button b1,b2,b3;

    private TextField t1,t2,t3;
    private String password;

    private Choice c1;
    private Font f1;
    private TextField t4;

    private Checkbox cb1,cb2;
    private TextField t5;

    private TextField t6;
    private CheckboxGroup radio;
    private Checkbox r1,r2,r3;

    private List colorList;

    public void init(){
    setSize(356,356);
    password="1234";
    b1=new Button("Click Here!");
    b2=new Button("Sorry,I don't do anything");
    b3=new Button();

    t1=new TextField("Enter PassWord!");
    t1.setEditable(false);

    t2=new TextField(12);
    t2.setEchoChar('*');

    t3=new TextField(30);
    t3.setEditable(false);

    c1=new Choice();
    c1.addItem("TimesRoman");
    c1.addItem("Courier");
    c1.addItem("Helvetica");
    t4=new TextField("Sample Text",16);
    t4.setEditable(false);
    f1=new Font(c1.getItem(0),Font.PLAIN,14);
    t4.setFont(f1);

      cb1=new Checkbox("Bold");
      cb2=new Checkbox();
      cb2.setLabel("Italic");
      t5=new TextField("Sample Text",30);
      
      t6=new TextField("Sample Text",40);
      radio=new CheckboxGroup();
      r1=new Checkbox("Plain",radio,true);
      r2=new Checkbox("Italic",radio,false);
      r3=new Checkbox("Bold",radio,false);
      
      colorList=new List(5,false);
      colorList.add("White");
      colorList.add("Black");
      colorList.add("Yellow");
      colorList.add("Green");
      
    add(b1);
    add(b2);
    add(b3);

    add(t1);
    add(t2);
    add(t3);

    add(c1);
    add(t4);

    add(t5);
    add(cb1);
    add(cb2);

    add(t6);
    add(r1);
    add(r2);
    add(r3);

    add(colorList);

    b1.addActionListener(this);
    b3.addActionListener(this);

    t2.addActionListener(this);

    c1.addItemListener(this);
    cb1.addItemListener(this);

    r1.addItemListener(this);
    r2.addItemListener(this);
    r3.addItemListener(this);

    colorList.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent e){
    if(e.getSource()==c1){

    if(e.getStateChange()==ItemEvent.SELECTED){
            
      f1=new Font(c1.getSelectedItem(),Font.PLAIN,14);
      t4.setFont(f1);
    }
    }  

    else if(e.getSource()==cb1){
    //如果选中CB1,变成BOLD字体,不知道怎么实现?
    }
    else if(e.getSource()==cb1){
    //如果选中CB1,变成ITALIC字体,不知道怎么实现?
    }

    else if(e.getSource()==r1){
    //Plain字体,不知道怎么实现?
    }

    else if(e.getSource()==r2){
    //Italic字体,不知道怎么实现?
    }

    else if(e.getSource()==r3){
    //Bold变粗体,不知道怎么实现?
    }

    else if(e.getSource()==colorList){
    //选中颜色,让applet背景变色,不知道怎么实现?
    }

    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==b1)
    showStatus("You had clicked b1!");
    else if(e.getSource()==b3)
    showStatus("You had clicked b3!");
    else if(e.getSource()==t2){
    if(t2.getText().equals(password))
    t3.setText("Access Granted!");
    else
    t3.setText("Wrong Password!"+"Access Denied!");
    }

     
      }
     


    }这个是新的,不知道怎么实现checkboxcheckboxGroup,和list的事件..唉.谁知道的说一下啊