void是变量paint的无效类型

时间:2014-06-30 12:29:01

标签: java applet

void是变量paint的无效类型是我通过单击复选框时我的程序获取的错误textfield显示textfield中的复选框名称谢谢! 使用if语句* ..........

package irt;

import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class chkBx extends Applet implements ItemListener
{
    TextField t1;
    Checkbox c1,c2,c3;
    Label l1;
    public void init()
    {
        l1=new Label("data");
        add(l1);
        t1=new TextField(15);
        add(t1);
        c1=new Checkbox("nashik");
        add(c1);
        c2=new Checkbox("pune");
        add(c2);
        c3=new Checkbox("mumbai");
        add(c3);
        c1.addItemListener(this);
        c2.addItemListener(this);
        c3.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent e) 
    {
        String msg="state";

                    public void paint(Graphics g)

                        {

                            t1.setText(msg);    
                            g.drawString(msg,50,100);
                            msg="city"+c1.getState();
                        }


    }
}

2 个答案:

答案 0 :(得分:1)

在Java中,您无法在方法中定义方法。

您需要在方法paint之外定义方法itemStateChanged

public void paint(Graphics g, String msg) {
    t1.setText(msg);    
    g.drawString(msg, 50, 100);
}

答案 1 :(得分:1)

方法public void paint(Graphics g)不属于方法itemStateChanged的一部分。

您必须更改代码:

public class chkBx extends Applet implements ItemListener
{
  String msg = new String();

  TextField t1;
  Checkbox c1, c2, c3;
  Label l1;

  public void init() {
    l1 = new Label("data");
    add(l1);
    t1 = new TextField(15);
    add(t1);
    c1 = new Checkbox("nashik");
    add(c1);
    c2 = new Checkbox("pune");
    add(c2);
    c3 = new Checkbox("mumbai");
    add(c3);
    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
  }

  public void itemStateChanged(ItemEvent e) {
    String msg = "state";
    this.repaint();
  }

  public void paint(Graphics g)

  {

    t1.setText(msg);
    g.drawString(msg, 50, 100);
    msg = "city" + c1.getState();
  }
}