A useful GUI . If you have the 10th edition of the Liang text, he deos a good jo
ID: 3597189 • Letter: A
Question
A useful GUI. If you have the 10th edition of the Liang text, he deos a good job of describing JavaFX. If you have the 9th ed., he describes using Swing. For the following exercise you can use either Swing or JavaFX.
Give me a clear indication of which you are using. DO NOT use/mix both.
Create an application that has a button that has “Enter your info.” When clicked it will present a dialog that has labels and text boxes that allow the user to enter their name, email, and phone number. The dialog will have buttons OK and Cancel.
When the user clicks Cancel, the dialog will go away without doing anything else.
When the user clicks OK, the user’s information will be extracted from the dialog, and dumped out on the console.
Notice that you only are listening for Click events.
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
JButton btnNewButton = new JButton("Enter Your Info");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
public class Registration extends JFrame implements ActionListener
{
JLabel l1, l2, l3,l4;
JTextField tf1, tf2, tf3;
JButton btn1, btn2;
Registration()
{
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
btn1 = new JButton("ok");
btn2 = new JButton("Cancel");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
tf3.setBounds(300, 150, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(l4);
add(tf2);
add(tf3);
add(btn1);
add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn1)
{
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
String s3 = tf3.getText();
if(e.getSource() == btn1)
{
System.out.printf(" Name: ", s1);
System.out.printf("Email: ", s2);
System.out.printf("Phone: ", s3);
}
if (e.getSource() == btn2)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
}
}
public static void main(String args[])
{
new Registration();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.