In: Computer Science
Use the below info to create a java program
A GUI interface to ensure a user is old enough to play a game.
Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this.
Instructions to ensure that the information is displayed back to the user. Also, the user must be 21. So, include instructions to display an appropriate message about whether or not the user can continue.
Create instructions that will allow another user to enter their information.
Some form of looping is required for this assignment.
Make sure to add additional code to show understanding of the GUI code.
Include comments in you code.
Solution:
Library:
import javax.swing.JOptionPane;//to be imported for using JOptionPane
Code for new form:
public class game_login extends javax.swing.JFrame {
/**
* Creates new form game_login
*/
public game_login() {
initComponents();
}
Code for Submit button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int option;
String name=jTextField5.getText();//to get name
String address=jTextArea2.getText();//to get address
String phoneno=jTextField3.getText();//to get phoneno
int age=Integer.parseInt(jTextField6.getText());//to get age
if(age<21)//evaluates age value
{
JOptionPane.showMessageDialog(null,"Oooops...! \n User must be 21
old.You are not old enough to play a game");//To display a
message
option=JOptionPane.showConfirmDialog(null,"Want to Try
Again?");//To get the confirmation from the user
if(option==0)//if they click YES,it returns 0
{
jTextField5.setText("");
jTextField3.setText("");
jTextField6.setText("");
jTextArea1.setText("");
jTextArea2.setText("");
}
else//if they click NO,it returns 1
{
JOptionPane.showMessageDialog(null,"Thank You,Have a great
day");//To display a message
System.exit(0);//To exit from the application
}
}
else //when olold enough to play
{
jTextArea1.setText("Name\t: "+name+"\nAddress\t: "+address+"\nPhone
Number: "+phoneno+"\nAge\t: "+age);//information is displayed back
to the user
JOptionPane.showMessageDialog(null,"Congrats..!!!, You have
submitted the details successfully\nGo Ahead");//To display a
message
}
}
For Clear Button:
private void jButton4ActionPerformed(java.awt.event.ActionEvent
evt) {
jTextField5.setText("");
jTextField3.setText("");
jTextField6.setText("");
jTextArea1.setText("");
jTextArea2.setText("");
//To cleear all the entries made by the user
}
For Exit Button:
private void jButton5ActionPerformed(java.awt.event.ActionEvent
evt) {
System.exit(0);
//To exit from an application
}
main method and other GUI code:
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel
setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with
the default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Payroll_system.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Payroll_system.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Payroll_system.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Payroll_system.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new game_login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration
}