当用户在 JTextField 中键入员工的名字和姓氏(由空格分隔)时,员工的职位将显示在第二个 JTextField 中。包括两个 JLabel 来描述用于数据输入的 JTextFields,并包括第三个 JLabel,其中包含员工的头衔或如果找不到员工的匹配项则显示错误消息
一切正常,但是当我包含以下部分代码时,即使数组匹配,也只有错误消息“找不到员工”。没有这个语句代码工作正常。请帮助..谢谢
if(!(name.equalsIgnoreCase(empName[x]) || job.equalsIgnoreCase(jobName[x])))
{
errorortitle.setText("Employee not found");
}
package appletLesson;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
public class JEmployeeTitle extends JApplet implements ActionListener
{
String[] empName = {"James", "Tom", "Steve", "Barack", "John"};
String[] jobName = {"Blunt", "Kites", "Jobs", "Obama", "Smith"};
String[] jobTitle = {"Actor", "Buisness men", "CEO", "President", "Painter"};
JLabel fnameLabel = new JLabel("Enter First Name:");
JLabel lnameLabel = new JLabel("ENter Last Name:");
JButton button = new JButton("Submit");
JLabel errorortitle = new JLabel(" ");
JTextField fnameField = new JTextField(20);
JTextField lnameField = new JTextField(20);
Container con = getContentPane();
public void init()
{
con.setBackground(Color.YELLOW);
con.add(fnameLabel);
con.add(fnameField);
con.add(lnameLabel);
con.add(lnameField);
con.add(button);
con.add(errorortitle);
con.setLayout(new FlowLayout());
fnameField.addActionListener(this);
lnameField.addActionListener(this);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String name = fnameField.getText();
String job = lnameField.getText();
for(int x = 0; x < 5; x++)
{
if(name.equalsIgnoreCase(empName[x]) || job.equalsIgnoreCase(jobName[x]))
{
errorortitle.setText(jobTitle[x]);
}
if(!(name.equalsIgnoreCase(empName[x]) || job.equalsIgnoreCase(jobName[x])))
{
errorortitle.setText("Employee not found");
}
}
validate();
}
}