Okay, so I'm in an Intro to Java class, and we're currently covering applets. This assignment is giving me a hard time, it reads as follows:
"Create a JApplet that contains two parallel arrays that contain at least five employees' names and jobs. Allow the user to enter either a title or a name and to click a JButton to display the other. Include a JLabel to describe each JTextField."
I know the formatting of the JFrame is a bit awkward, but I'll fix that later, my main problem is that when someone enters something in the job title box and presses submit, it should give the name of the person with that job title, and vice versa. However, no matter what valid job title or name I enter into either JTextField, it always ends up reading "The Designer position is held by Susan" along with not removing the components that I want removed until it is minimized and brought back up. So it displays the wrong message, along with the removal problem.
Any help on this would be VERY appreciated, I've been trying really hard at it for the past two days and I'm having lots of trouble figuring it out!
Here is my HTML code:
<html> <object code = ”JEmployeeTitle2.class” width = 500 height = 500> </object> </html>
Here is my Java code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JEmployeeTitle2 extends JApplet implements ActionListener { JLabel jobLabel = new JLabel("Enter a job title below."); JLabel employeeLabel = new JLabel("Enter an employee name below."); Font font1 = new Font("Times New Roman", Font.BOLD, 20); JTextField jobTextField = new JTextField(20); JTextField employeeTextField = new JTextField(20); JButton submit = new JButton("Submit"); JLabel lastLabel = new JLabel(" "); Container con = getContentPane(); public void init() { jobLabel.setFont(font1); employeeLabel.setFont(font1); con.add(jobLabel); con.add(employeeLabel); con.add(jobTextField); con.add(employeeTextField); con.add(submit); con.setLayout(new FlowLayout()); submit.addActionListener(this); jobTextField.addActionListener(this); employeeTextField.addActionListener(this); } public void actionPerformed(ActionEvent e) { boolean valid = false; String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"}; String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"}; while(valid == false) { for(int x = 0; x < jobTitle.length; x++) { if(jobTextField.getText() == jobTitle[x]) { valid = true; con.remove(jobLabel); con.remove(employeeLabel); con.remove(jobTextField); con.remove(employeeTextField); con.remove(submit); lastLabel.setText("The " + jobTitle[x] + " position is held by " + employeeName[x]); lastLabel.setFont(font1); con.add(lastLabel); } if(employeeTextField.getText() == employeeName[x]); { valid = true; con.remove(jobLabel); con.remove(employeeLabel); con.remove(jobTextField); con.remove(employeeTextField); con.remove(submit); lastLabel.setText(employeeName[x] + " holds the position of " + jobTitle[x]); con.add(lastLabel); } } } invalidate(); validate(); } }