regarding error in java program

Discussion in 'Java' started by udayshri, Mar 26, 2008.

  1. udayshri

    udayshri

    Joined:
    Mar 26, 2008
    Messages:
    1
    Likes Received:
    0
    hello everybody,

    I am learning java programming and i have tried to make frame and add buttons to it but I am facing problem while adding actionlistener to it. following is my code. kindly help me in this. thank you...

    ---------------------------------------------------------------------------
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    class Project extends JFrame implements ActionListener{
    public static void main(String[] args)
    {
    JFrame frame = new JFrame("FrameDemo");
    JButton Addition = new JButton("Addition");
    Addition.addActionListener(this);
    frame.add(Addition);
    JTextField txt1 = new JTextField("First", 15);
    frame.add(txt1);
    JTextField txt2 = new JTextField("Second", 15);
    frame.add(txt2);

    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    frame.setSize(170, 100);
    frame.setLayout(new FlowLayout());
    frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {

    txt1.setText("Button Clicked ");
    }

    }
    --------------------------------------------------------------------------

    and the error I am getting are

    Project.java:17: non-static variable this cannot be reference
    text
    Addition.addActionListener(this);
    ^
    Project.java:35: cannot resolve symbol
    symbol : variable txt1
    location: class Project
    txt1.setText("Button Clicked ");
    ^
    2 errors
     
    udayshri, Mar 26, 2008
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.