import java.awt.*;
import java.awt.event.*;public class FocusDemo extends Frame 
implements ActionListener,FocusListener,ItemListener
{
Label labelname;
Label labelsex;
Label labellike;
TextField text;
CheckboxGroup chg;
Checkbox boxman;
Checkbox boxwoman;

Checkbox boxfootball;
Checkbox boxbasketball;
Checkbox boxswim;
Checkbox boxmusic;

Button butok;
Button butcanel;
Panel panel;
String str;
GridBagLayout gb;
GridBagConstraints gbc;
public FocusDemo()
{
super("FocusDemo");
panel=new Panel();
this.add(panel);

gb=new GridBagLayout();
panel.setLayout(gb);
gbc=new GridBagConstraints();

labelname=new Label("用户名:");
labelsex=new Label("性别:");
labellike=new Label("爱好:");

text=new TextField();
text.addFocusListener(this);

chg=new CheckboxGroup();
boxman=new Checkbox("男",chg,false);
boxman.addItemListener(this);
boxwoman=new Checkbox("女",chg,false);
boxwoman.addItemListener(this);

boxfootball=new Checkbox("足球",false);
boxfootball.addItemListener(this);
boxbasketball=new Checkbox("篮球",false);
boxbasketball.addItemListener(this);
boxswim=new Checkbox("游泳",false);
boxswim.addItemListener(this);
boxmusic=new Checkbox("音乐",false);
boxmusic.addItemListener(this);

butok=new Button("确定");
butok.addActionListener(this);
butcanel=new Button("取消");
butcanel.addActionListener(this);

gbc.fill=GridBagConstraints.BOTH;
addComponent(labelname,0,0,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(text,1,0,2,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(labelsex,0,1,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxman,1,1,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxwoman,2,1,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(labellike,2,0,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxfootball,1,2,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxbasketball,2,2,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxswim,1,3,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(boxmusic,2,3,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(butok,0,4,1,1);
gbc.fill=GridBagConstraints.BOTH;
addComponent(butcanel,2,4,1,1);
this.pack();
this.setVisible(true);
this.setSize(600,250);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void addComponent(Component c,int x,int y,int width,int height)
{
gbc.gridx=x;
gbc.gridy=y;

gbc.gridwidth=width;
gbc.gridheight=height;

gbc.weightx=0;
gbc.weighty=0;
gb.setConstraints(c,gbc);
panel.add(c);
}
public void itemStateChanged(ItemEvent e)
{

if(e.getSource()==boxman)
{
str.append(boxman);
}
if(e.getSource()==boxwoman)
{
str.append(boxwoman);
}
if(e.getSource()==boxfootball)
{
str.append(boxfootball);
}
if(e.getSource()==boxbasketball)
{
str.append(boxbasketball);
}
if(e.getSource()==boxswim)
{
str.append(boxswim);
}
if(e.getSource()==boxmusic)
{
str.append(boxmusic);
}
}
public void focusGained(FocusEvent e)
{
String struser=text.getText();
if(struser.length()<6||struser.length()>10)
{
System.out.println("不合法的用户名");
//
}
}
public void focusLost(FocusEvent e)
{
System.out.println("lost FocusEvent");
}
public void actionPerformed(ActionEvent e)
{


if(e.getSource()==butok)
{
//System.out.println(str);
}
if(e.getSource()==butcanel)
{
//System.out.println("没有内容输出");
}
}
public static void main(String args[])
{
new FocusDemo();
}

我想实现把用户选择的信息打印出来,但事件处理有问题,想求教一下

解决方案 »

  1.   

    这样可以了
    import java.awt.*;
    import java.awt.event.*;public class FocusDemo extends Frame 
    implements ActionListener,FocusListener,ItemListener
    {
    Label labelname;
    Label labelsex;
    Label labellike;
    TextField text;
    CheckboxGroup chg;
    Checkbox boxman;
    Checkbox boxwoman;

    Checkbox boxfootball;
    Checkbox boxbasketball;
    Checkbox boxswim;
    Checkbox boxmusic;

    Button butok;
    Button butcanel;
    Panel panel;
    StringBuffer str=new StringBuffer();//改为StringBuffer,并实例化
    GridBagLayout gb;
    GridBagConstraints gbc;
    public FocusDemo()
    {
    super("FocusDemo");
    panel=new Panel();
    this.add(panel);

    gb=new GridBagLayout();
    panel.setLayout(gb);
    gbc=new GridBagConstraints();

    labelname=new Label("用户名:");
    labelsex=new Label("性别:");
    labellike=new Label("爱好:");

    text=new TextField();
    text.addFocusListener(this);

    chg=new CheckboxGroup();
    boxman=new Checkbox("男",chg,false);
    boxman.addItemListener(this);
    boxwoman=new Checkbox("女",chg,false);
    boxwoman.addItemListener(this);

    boxfootball=new Checkbox("足球",false);
    boxfootball.addItemListener(this);
    boxbasketball=new Checkbox("篮球",false);
    boxbasketball.addItemListener(this);
    boxswim=new Checkbox("游泳",false);
    boxswim.addItemListener(this);
    boxmusic=new Checkbox("音乐",false);
    boxmusic.addItemListener(this);

    butok=new Button("确定");
    butok.addActionListener(this);
    butcanel=new Button("取消");
    butcanel.addActionListener(this);

    gbc.fill=GridBagConstraints.BOTH;
    addComponent(labelname,0,0,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(text,1,0,2,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(labelsex,0,1,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxman,1,1,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxwoman,2,1,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(labellike,2,0,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxfootball,1,2,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxbasketball,2,2,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxswim,1,3,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(boxmusic,2,3,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(butok,0,4,1,1);
    gbc.fill=GridBagConstraints.BOTH;
    addComponent(butcanel,2,4,1,1);
    this.pack();
    this.setVisible(true);
    this.setSize(600,250);
    this.addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    }
    public void addComponent(Component c,int x,int y,int width,int height)
    {
    gbc.gridx=x;
    gbc.gridy=y;

    gbc.gridwidth=width;
    gbc.gridheight=height;

    gbc.weightx=0;
    gbc.weighty=0;
    gb.setConstraints(c,gbc);
    panel.add(c);
    }
    public void itemStateChanged(ItemEvent e)
    {

    if(e.getSource()==boxman)
    {
    str.append(boxman.getLabel());//getLabel
    }
    if(e.getSource()==boxwoman)
    {
    str.append(boxwoman.getLabel());
    }
    if(e.getSource()==boxfootball)
    {
    str.append(boxfootball.getLabel());
    }
    if(e.getSource()==boxbasketball)
    {
    str.append(boxbasketball.getLabel());
    }
    if(e.getSource()==boxswim)
    {
    str.append(boxswim.getLabel());
    }
    if(e.getSource()==boxmusic)
    {
    str.append(boxmusic.getLabel());
    }
    }
    public void focusGained(FocusEvent e)
    {
    String struser=text.getText();
    if(struser.length()<6||struser.length()>10)
    {
    System.out.println("不合法的用户名");
    //
    }
    }
    public void focusLost(FocusEvent e)
    {
    System.out.println("lost FocusEvent");
    str.append(text.getText());//
    }
    public void actionPerformed(ActionEvent e)
    {


    if(e.getSource()==butok)
    {
    System.out.println(str);
    }
    if(e.getSource()==butcanel)
    {
    System.out.println("没有内容输出");
    }
    }
    public static void main(String args[])
    {
    new FocusDemo();
    }