In: Computer Science
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
//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: