Answer:
Check the explanation
Step-by-step explanation:
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class nameFrame {
private JFrame mainFrame;
public nameFrame(){
prepareGUI();
}
public static void main(String[] args){
nameFrame nameFrame = new nameFrame();
}
private void prepareGUI(){
mainFrame = new JFrame("Name Frame");
mainFrame.setSize(450,250);
mainFrame.setLayout(new GridLayout(5, 2));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
JLabel fname= new JLabel("First Name : ");
JLabel lname = new JLabel("Last Name : ");
JLabel fullname = new JLabel("Full Name : ");
JTextField fnameText = new JTextField(15);
JTextField lnameText = new JTextField(15);
JTextField fullnameText = new JTextField(30);
fullnameText.setEditable(false);
JButton submit = new JButton("submit");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = fnameText.getText() + " " + lnameText.getText();
fullnameText.setText(data);
}
});
JButton clear = new JButton("clear");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fnameText.setText("");
lnameText.setText("");
fullnameText.setText("");
}
});
mainFrame.add(fname);
mainFrame.add(fnameText);
mainFrame.add(lname);
mainFrame.add(lnameText);
mainFrame.add(fullname);
mainFrame.add(fullnameText);
mainFrame.add(submit);
mainFrame.add(clear);
mainFrame.setVisible(true);
}
}
Kindly check the attached output image below.