You can find the complete details about Java Swing Components example with Action source code. The program for Java Swing Components which implements ActionListener.
The following program will help you to create a window and show label, text box, button. You can enter some content in that text box and press on submit button. The message box will get populate amd show you the text box content.
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SwingComponentwithAction extends JFrame implements ActionListener {
JFrame frame1;
JLabel l1;
JTextField t1;
JButton b1;
JOptionPane jop1;
public SwingComponentwithAction(){
frame1=new JFrame();
Container cp=frame1.getContentPane();
cp.setLayout(null);
l1=new JLabel(“Enter your name:”);
l1.setBounds(40,30,100,30);
cp.add(l1);
t1=new JTextField(20);
t1.setBounds(160,30,200,30);
cp.add(t1);
b1=new JButton(“Submit”);
b1.setBounds(140,150,80,30);
b1.addActionListener(this); // this is new compare with SwingComponent.java
cp.add(b1);
frame1.setSize(400,300);
frame1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
String name=t1.getText();
jop1=new JOptionPane();
jop1.showMessageDialog(frame1,”Hello “+name);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SwingComponentwithAction();
}
}
You should save this program with name called SwingComponentwithAction.java then compile and run this program to get output.