import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUIClass extends JFrame {
public static void main (String[] args){
// TODO Auto-generated method stub
JFrame fm = new JFrame();
final JTextField tf = new JTextField();
JButton butn = new JButton("Click ME");
tf.setBounds(50, 20, 130, 40);
butn.setBounds(50, 80, 120, 30);
butn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(this, tf.getText());
}
});
fm.add(tf);
fm.add(butn);
fm.setLayout(null);
fm.setVisible(true);
fm.setSize(400, 300);
}
}
So the problem lies within...
butn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(this, tf.getText());
}
});
this
, in this context, refers to the anonymous instance of the ActionListener
, not the parent class.
A simple solution might be to use JOptionPane.showMessageDialog(tf, tf.getText());