Swing Java员工计划

时间:2013-08-07 01:16:50

标签: java swing

当用户在a中键入员工的名字和姓氏(用空格分隔)时 JTextField,员工的职位名称显示在secondJTextField中。包括 twoJLabel描述用于数据输入的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(); 
} 
}

1 个答案:

答案 0 :(得分:1)

你的问题就在这个循环中:

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");

    } 
}

一旦找到匹配项,您应该突破循环,否则您将继续浏览所有名称并用“未找到员工”覆盖正确的jobTitle。 (我打赌你的原始代码可以和John Smith一起正常工作 - 没有其他人 - 试试吧。)所以,例如:

for(int x = 0; x < 5; x++) 
{ 
    if(name.equalsIgnoreCase(empName[x]) ||           job.equalsIgnoreCase(jobName[x]))
    { 
        errorortitle.setText(jobTitle[x]);
        break; // stop checking
    } 
    if(!(name.equalsIgnoreCase(empName[x]) ||    job.equalsIgnoreCase(jobName[x])))
    { 
        errorortitle.setText("Employee not found");

    } 
}

我也想知道你的意思是&&而不是||,但我不确切地知道你的目的是什么。

在这种情况下,只需坐下来仔细查看您的代码即可。一次一行地(或使用调试器)在头脑中逐步执行它,看看你是否能发现问题。如果它有帮助,请在纸上写出迭代。

希望有所帮助。

顺便说一句,是的,有更简洁的方法来编写上面的循环。我将把它作为练习留给OP。