A useful GUI . If you have the 10th edition of the Liang text, he deos a good jo
ID: 3597187 • 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
Person.java
package com;
public class Person {
private String Name;
private String Email;
private long PhoneNumber;
Person(String name, String email, long phonenumber)
{
Name = name;
Email = email;
PhoneNumber = phonenumber;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public long getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
PhoneNumber = phoneNumber;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Name=" + Name + " Email=" + Email + " PhoneNumber=" + PhoneNumber;
}
}
PersonGUI.java
package com;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class PersonGUI {
public PersonGUI() {
JFrame frame = new JFrame("Person");
JButton enterInfo = new JButton("Enter your info");
JButton sort = new JButton("Sort the list");
List<Person> listOfPersons = new ArrayList<Person>();
enterInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String name = JOptionPane.showInputDialog("Enter Name: ");
String email = JOptionPane.showInputDialog("Enter email: ");
long number = 0;
number = Long.valueOf(JOptionPane.showInputDialog("Enter phone number: "));
if(name != null && email != null && number != 0)
{
listOfPersons.add(new Person(name, email, number));
System.out.print("Info Added: ");
System.out.println(new Person(name, email, number));
}
}
});
sort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(listOfPersons, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
});
System.out.println(" Sorted List: ");
for(int i=0; i<listOfPersons.size(); i++)
{
System.out.println(listOfPersons.get(i));
}
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(enterInfo);
frame.getContentPane().add(sort);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String args[])
{
new PersonGUI();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.