Question:
The program should display the message "Successfully Submitted" on the console when clicked on the "Submit" button and the text should get cleared when the user clicks on "Clear" button.
Program:
import java.awt.*;
import java.awt.event.*;
public class extraRegistrationForm {
public static void main(String[] args) {
Frame f = new newRegistrationFrame("Registration Form");
f.setSize(250, 300);
f.setVisible(true);
}
}
//Frame class
class newRegistrationFrame extends Frame {
TextField t1,t2;
public newRegistrationFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
Label l1=new Label("Enter your name:");
l1.setBounds(10, 40, 120, 50);
Label l2=new Label("Enter your Mobile no:");
l2.setBounds(10, 70, 120, 50);
add(l1);
add(l2);
t1=new TextField();
t1.setEditable(true);
t1.setBounds(130, 55, 100, 20);
t2=new TextField();
t2.setBounds(130, 85, 100, 20);
t2.setEditable(true);
add(t1);
add(t2);
//Add button to frame and attach listener
Button b = new Button("Submit");
b.setBounds(30, 150, 100, 30);
add(b);
b.addActionListener(new ButtonListener());
Button b1 = new Button("Clear");
b1.setBounds(140, 150, 100, 30);
add(b1);
b1.addActionListener(new ButtonListener());
//Attach window listener
addWindowListener(new WindowCloser());
}
//Listener for button
class ButtonListener implements ActionListener {
Label t=new Label();
public void actionPerformed(ActionEvent evt) {
if(evt.getActionCommand().equals("Submit")) {
//If we Have Print It On Console
System.out.println("Successfully Submitted");
/*
If we have print It on The Awt Window
t.setText("Successfully Submitted");
t.setBounds(100, 210, 150, 30);
add(t);
*/
System.out.println("Successfully Submitted");
/*
If we have print It on The Awt Window
t.setText("Successfully Submitted");
t.setBounds(100, 210, 150, 30);
add(t);
*/
}
else if(evt.getActionCommand().equals("Clear")) {
t1.setText("");
t2.setText("");
}
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Credits: Arjun
0 comments:
Post a Comment