CODE:
package swing_practicals;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Swing_Practicals extends JFrame implements ActionListener{
JLabel lblname;
JTextField txtname;
JButton btnok;
JPanel pnl;
Swing_Practicals(){
setVisible(true);
setSize(500,500);
setTitle("Kiran");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblname = new JLabel("Enter your name:");
txtname = new JTextField(15);
btnok = new JButton("OK");
pnl = new JPanel();
pnl.add(lblname);
pnl.add(txtname);
pnl.add(btnok);
btnok.addActionListener(this);
getContentPane().add(pnl,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent ae){
String str = txtname.getText();
JOptionPane.showMessageDialog(this,"Hi welcome "+str);
txtname.setText("");
}
public static void main(String[] args) {
new Swing_Practicals();
}
}
OUTPUT:
0 Comments