This code is compiling and executing too, but the method actionPerformed()
is not executing properly. I mean after clicking on the OK Button, there is nothing written in the JTextField
. NO action is performed even after using e.getSource()
. The System.out.println("I am done ")
is working properly, but t.setText("Hey there")
is not working.? What is wrong with the code?? Please help me if anyone can.
And also can you elaborate me why if not adding JButton
and JTextField
on Panel
it is not visible? Why it is important to add panel in order to make button and text field visible. Without it is not visible why?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class A implements ActionListener {
JFrame f;
JButton b;
JPanel p;
JLabel l;
JTextField t;
A(String s) {
JFrame f=new JFrame(s);
f.setVisible(true);
f.setSize(400,400);
JButton b= new JButton("OK");
JTextField t=new JTextField();
JPanel p=new JPanel();
f.add(p);
p.setBounds(0,0,300,300);
p.add(b);
b.setBounds(30,40,80,80);
p.add(t);
t.setBounds(100,200,80,80);
b.addActionListener(this);
t.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
t.setText("Hey There");
}
System.out.println("I m done!!");
}
public static void main(String[] args) {
System.out.println("Hey there");
new A("First App");
}
}