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
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