Question

In: Computer Science

Im trying to get a GUI interface in java where there is four text fields for...

Im trying to get a GUI interface in java where there is four text fields for the first name, last name, department, and phone number of employee in a program. Then also have a radio button below for Gender (Male/Female/Other) and a list for the Title (Mr./Ms./Mrs./Dr./Col./Prof.). At the very bottom of the frame there has to be buttons for printing, submitting and exiting but for whatever reason when I tried it nothing appears in the frame regardless of what I do. I need help coding this its been a while since I've used GUI interface and I don't remember all of the syntax anymore

Solutions

Expert Solution

//GUI.java
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI extends JFrame {

   private JPanel contentPane;
   private JPanel panel;
   private JTextArea txtrLastName;
   private JTextField textField;
   private JTextArea txtrFirstName;
   private JTextField textField_1;
   private JTextArea txtrDepartment;
   private JTextField textField_2;
   private JTextArea txtrPhoneNumber;
   private JTextField textField_3;
   private final ButtonGroup buttonGroup = new ButtonGroup();
   private Directory d = new Directory();

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   GUI frame = new GUI();
                   frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the frame.
   */
   public GUI() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 450, 300);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(new GridLayout(0, 1, 0, 0));
      
       panel = new JPanel();
       contentPane.add(panel);
       panel.setLayout(null);
      
       txtrFirstName = new JTextArea();
       txtrFirstName.setEditable(false);
       txtrFirstName.setBounds(17, 4, 100, 22);
       txtrFirstName.setText("First Name: ");
       panel.add(txtrFirstName);
      
       textField = new JTextField();
       textField.setBounds(127, 6, 86, 20);
       panel.add(textField);
       textField.setColumns(10);
      
       txtrLastName = new JTextArea();
       txtrLastName.setEditable(false);
       txtrLastName.setBounds(218, 5, 92, 22);
       txtrLastName.setText("Last Name: ");
       panel.add(txtrLastName);
      
       textField_1 = new JTextField();
       textField_1.setBounds(315, 6, 86, 20);
       panel.add(textField_1);
       textField_1.setColumns(10);
      
       txtrDepartment = new JTextArea();
       txtrDepartment.setEditable(false);
       txtrDepartment.setBounds(17, 37, 100, 22);
       txtrDepartment.setText("Department: ");
       panel.add(txtrDepartment);
      
       textField_2 = new JTextField();
       textField_2.setBounds(127, 37, 86, 20);
       panel.add(textField_2);
       textField_2.setColumns(10);
      
       txtrPhoneNumber = new JTextArea();
       txtrPhoneNumber.setEditable(false);
       txtrPhoneNumber.setBounds(218, 37, 92, 22);
       txtrPhoneNumber.setText("Phone: ");
       panel.add(txtrPhoneNumber);
      
       textField_3 = new JTextField();
       textField_3.setBounds(315, 37, 86, 20);
       panel.add(textField_3);
       textField_3.setColumns(10);
      
       JTextArea txtrGender = new JTextArea();
       txtrGender.setEditable(false);
       txtrGender.setText("Gender: ");
       txtrGender.setBounds(17, 70, 100, 22);
       panel.add(txtrGender);
      
       final JRadioButton rdbtnMale = new JRadioButton("Male");
       buttonGroup.add(rdbtnMale);
       rdbtnMale.setSelected(true);
       rdbtnMale.setBounds(127, 70, 86, 23);
       panel.add(rdbtnMale);
      
       final JRadioButton rdbtnFemale = new JRadioButton("Female");
       buttonGroup.add(rdbtnFemale);
       rdbtnFemale.setBounds(218, 70, 92, 23);
       panel.add(rdbtnFemale);
      
       JRadioButton rdbtnOther = new JRadioButton("Other");
       buttonGroup.add(rdbtnOther);
       rdbtnOther.setBounds(315, 70, 86, 23);
       panel.add(rdbtnOther);
      
       JTextArea txtrTitle = new JTextArea();
       txtrTitle.setEditable(false);
       txtrTitle.setText("Title: ");
       txtrTitle.setBounds(17, 110, 100, 22);
       panel.add(txtrTitle);
      
       Object[] titles = {"Mr.", "Ms.", "Mrs.", "Dr.", "Col.", "Prof."};
       final JList list = new JList(titles);
       list.setBounds(127, 110, 274, 119);
       panel.add(list);
      
       JButton btnSubmit = new JButton("Submit");
       btnSubmit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
              
               String lastName, firstName, department, phone, gender, title;
               firstName = textField.getText();
               lastName = textField_1.getText();
               department = textField_2.getText();
               phone = textField_3.getText();
               if(rdbtnMale.isSelected()){
                   gender = "Male";
               }
               else if(rdbtnFemale.isSelected()){
                   gender = "Female";
               }
               else gender = "Other";
               title = (String) list.getSelectedValue();
              
               Employee e = new Employee(firstName, lastName, department, phone, gender, title);
               d.add(e);
           }
       });
       btnSubmit.setBounds(10, 143, 89, 23);
       panel.add(btnSubmit);
      
       JButton btnExit = new JButton("Exit");
       btnExit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
           }
       });
       btnExit.setBounds(10, 177, 89, 23);
       panel.add(btnExit);
   }
}

===========================================================================

//Employee .java
public class Employee {
   private String f,l,d,p,g,t;
   public Employee(String f, String l, String d, String p, String g, String t){
       this.f = f;
       this.l = l;
       this.d = d;
       this.p = p;
       this.g = g;
       this.t= t;
   }
  
   public String getF(){
       return this.f;
   }
  
   public String getL(){
       return this.l;
   }
  
   public String getD(){
       return this.d;
   }
  
   public String getP(){
       return this.p;
   }
  
   public String getG(){
       return this.g;
   }
  
   public String getT(){
       return this.t;
   }
  
   public String toString(){
       return (t+" "+f+" "+l+" "+d+" "+p+" Gender: "+g);
   }
}

========================================

//Directory.java

import java.util.ArrayList;
public class Directory {
   private ArrayList<Employee> dir;
  
   public Directory(){
       this.dir = new ArrayList<Employee>();
   }
  
   public void add(Employee e){
       dir.add(e);
   }
  
   public void print(){
       System.out.println(dir);
   }
  
   public void clear(){
       this.dir = new ArrayList<Employee>();
   }
}

======================================================================

sample output:


Related Solutions

I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
Use the below info to create a java program A GUI interface to ensure a user...
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...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
*In JAVA and JavaFX please!! CSE1322 – Assignment 8 – GUI General User Interface Assignment 8...
*In JAVA and JavaFX please!! CSE1322 – Assignment 8 – GUI General User Interface Assignment 8 Objectives • Build a GUI application similar to a calculator. • Create an application that acts as a simple calculator. Create buttons for 0-9 and a text field that displays the concatenation of the current digits as they are clicked. • Add buttons for operators “+”, “-“, “*”, and “/”. • Add a final button for “=” that calculates the computed value with the...
im trying to write a java code that take a matrix of vector and fine it's...
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector (matrix)-1 × ( vector ) = (vector)
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
Create a very simple temperature converter form having two text fields. The first one is where...
Create a very simple temperature converter form having two text fields. The first one is where the user will enter temperature. The second field is where the computed temperature value is displayed depending on the unit (F or C).use html and javascript
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT