In: Computer Science
In Java:
1) Create a new eclipse project.
2) Create a basic SWING window
3) Create a Label Called Name
4) Create a Text Field for entering the name
5) Create a Text Field for entering and email
5) Create a text area to push the results to
6) Create two buttons, one that says submit and the other that says clear.
7) When the user enters their name and email, they should press submit and see the Text area populate with the data they submitted.
8) When the user presses clear it should clear any text from all fields.
Code:
//importing sswing package
import javax.swing.*;
//as we are using event listeners
//we need to import awt.event package
import java.awt.event.*;
//class name is Frame, so the file should be named as
Frame.java
class Frame{
//main method
public static void main(String[] args) {
//creating an istance of
frame
JFrame f = new JFrame();
//a label
JLabel name = new JLabel("Name:
");
//seting boundaries
name.setBounds(50,50,100,30);
JLabel email = new JLabel("E-mail:
");
email.setBounds(50,100,100,30);
//textfield
JTextField tf = new
JTextField();
JTextField tf2 = new
JTextField();
tf.setBounds(100,50,100,30);
tf2.setBounds(100,100,100,30);
//buttons
JButton b1 = new
JButton("Submit");
JButton b2 = new
JButton("Clear");
b1.setBounds(50,150,100,50);
b2.setBounds(200,150,100,50);
//textArea
JTextArea ta = new
JTextArea();
ta.setBounds(50,250,300,100);
//adding all the components to the
frame
f.add(name);
f.add(email);
f.add(tf);
f.add(tf2);
f.add(b1);
f.add(b2);
f.add(ta);
//setting action listeners for
buttons
//submit
b1.addActionListener(new
ActionListener(){
public void
actionPerformed(ActionEvent e){
//collecting textfrom textfields
String s1 = tf.getText();
String s2 = tf2.getText();
//putting it in text Area
ta.setText("Name: "+s1+"\nE-mail: "+s2);
}
});
//clear
b2.addActionListener(new
ActionListener(){
public void
actionPerformed(ActionEvent e){
//setting all the fields to empty
ta.setText("");
tf.setText("");
tf2.setText("");
}
});
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
When clicked submit:
When clicked clear:
Code Screnshot:
Code Snippet:
//importing sswing package
import javax.swing.*;
//as we are using event listeners
//we need to import awt.event package
import java.awt.event.*;
//class name is Frame, so the file should be named as Frame.java
class Frame{
//main method
public static void main(String[] args) {
//creating an istance of frame
JFrame f = new JFrame();
//a label
JLabel name = new JLabel("Name: ");
//seting boundaries
name.setBounds(50,50,100,30);
JLabel email = new JLabel("E-mail: ");
email.setBounds(50,100,100,30);
//textfield
JTextField tf = new JTextField();
JTextField tf2 = new JTextField();
tf.setBounds(100,50,100,30);
tf2.setBounds(100,100,100,30);
//buttons
JButton b1 = new JButton("Submit");
JButton b2 = new JButton("Clear");
b1.setBounds(50,150,100,50);
b2.setBounds(200,150,100,50);
//textArea
JTextArea ta = new JTextArea();
ta.setBounds(50,250,300,100);
//adding all the components to the frame
f.add(name);
f.add(email);
f.add(tf);
f.add(tf2);
f.add(b1);
f.add(b2);
f.add(ta);
//setting action listeners for buttons
//submit
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//collecting textfrom textfields
String s1 = tf.getText();
String s2 = tf2.getText();
//putting it in text Area
ta.setText("Name: "+s1+"\nE-mail: "+s2);
}
});
//clear
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//setting all the fields to empty
ta.setText("");
tf.setText("");
tf2.setText("");
}
});
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}