theRadBrad
theRadBrad

Reputation: 125

void is an invalid type for the variable paint

void is an invalid type for the variable paint is the error i am getting by the way my program is when you click the checkbox the textfield shows the checkbox name in textfield thank you! Using if statement*..........

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();
                        }


    }
}

Upvotes: 1

Views: 3541

Answers (2)

Jens
Jens

Reputation: 69450

The method public void paint(Graphics g) con not be a part of the method itemStateChanged.

You have to change your code:

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();
  }
}

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53829

In Java, you cannot define a method inside a method.

You need to define the method paint outside of the method itemStateChanged:

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

Upvotes: 1

Related Questions