我正在尝试构建一个工资单系统而我在使用变量和动作监听器时遇到了麻烦,不知何故,它无法看到我在actionlistener中声明的变量,我&# 39;我试过在课堂上使用它,但我不确定我做错了什么因为它没有用。提前谢谢!
这是我的代码:
import java.io.File; import java.io.FileNotFoundException; import
java.util.ArrayList; import java.util.List; import
java.util.Scanner; import javax.swing.*; import java.awt.*; import
java.awt.event.*;
@SuppressWarnings("serial")
public class kapoy extends JFrame{
public static JTextField text1;
public static JTextField text2;
public JLabel label1;
public JLabel label2;
public JPanel panel1;
public JPanel panel2;
public JPanel panel3;
public JPanel panel4;
public kapoy() {
text1 = new JTextField();
text1.setPreferredSize(new Dimension(50,20));
text2 = new JTextField();
text2.setPreferredSize(new Dimension(50,20));
label1 = new
JLabel("Inpute Employee ID: ");
label2 = new JLabel("Input workedDays: ");
panel1 = new JPanel();
panel1.setLocation(0,0);
panel1.setSize(300,40);
panel1.setBackground(Color.blue);
panel1.add(label1);
panel1.add(text1);
add(panel1);
panel2 = new JPanel();
panel2.setLocation(0,40);
panel2.setSize(300,40);
panel2.setBackground(Color.red);
panel2.add(label2);
panel2.add(text2);
add(panel2);
panel3 = new JPanel();
panel3.setLocation(0,80);
panel3.setSize(400,200);
panel3.setBackground(Color.green);
add(panel3);
panel4 = new JPanel();
panel4.setLocation(300,0);
panel4.setSize(100,80);
panel4.setBackground(Color.yellow);
add(panel4);
setSize(410,300);
setLayout(null);
setTitle("Pay Roll by Migz"); }
public static void main(String[] args) {
kapoy cn = new kapoy(); cn.setVisible(true);
text1.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e)
{
try {
/**Cant use this-->**/int x = Integer.parseInt(text1.getText());
} catch (NumberFormatException nfe) {
text1.setText("");
}
} });
try {
File f = new File("D:/Users/DAVID Family/Desktop/Employees.txt");
Scanner sc = new Scanner(f);
List<Employee> people = new ArrayList<Employee>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = line.split(" ");
int Id = Integer.parseInt(details[0]);
String name = details[1];
int rate = Integer.parseInt(details[2]);
Employee p = new Employee(Id, name, rate);
/**in here-->**/if (x == Id){
people.add(p);
}
}
for(Employee p: people){
System.out.println(p.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Employee{
private int Id;
private String name;
private int rate;
public Employee(int Id, String name, int rate){
this.Id = Id;
this.setName(name);
this.rate = rate;
}
public int getId() {
return Id; }
public void setId(int Id)
{
this.Id = Id; }
/public void setName(String
name) {
this.name = name; }
public String getName() {
return name; }
public int getrate() {
return rate; }
public void setrate(int rate) {
this.rate = rate; }
public String toString(){
return this.Id + " " + this.name + " " + this.rate; } }
答案 0 :(得分:1)
您定义的变量int x = Integer.parseInt(text1.getText());
是方法keyReleased
的 local 变量,因为您已在方法中定义它,因此此变量的范围始终在那种方法。
这意味着您只能在方法内部使用此变量,而不能在其外部使用。
如果您想在课堂上使用它,即在该方法之外,那么您应该尝试使用一些实例变量。
答案 1 :(得分:1)
不要使用KeyListener
来修改字段的状态,这可能会导致并发修改异常,在通知Document
之前可以修改基础KeyListener
并且不会考虑到用户将文字粘贴到您的字段中会发生什么。
相反,如果您想要限制实时输入的字段,请使用DocumentFilter
和/或DocumentListener
如果您希望在字段发生更改时收到通知,请{39} ; s Document
查看Implementing a Document Filter,DocumentFilter Examples和Listening for Changes on a Document了解详情
您还需要了解您在事件驱动的环境中进行操作。这意味着用户操作可以在任何时间以任何顺序发生。你唯一能做的就是等到某个事件发生并回复。
这意味着像......
kapoy cn = new kapoy();
cn.setVisible(true);
int x = -1;
text1.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
try {
x = Integer.parseInt(text1.getText());
} catch (NumberFormatException nfe) {
text1.setText("");
}
}
});
try {
File f = new File("D:/Users/DAVID Family/Desktop/Employees.txt");
Scanner sc = new Scanner(f);
List<Employee> people = new ArrayList<Employee>();
while (sc.hasNextLine()) {
//...
Employee p = new Employee(Id, name, rate);
if (x == Id) {
people.add(p);
}
}
//...
} catch (FileNotFoundException e) {
e.printStackTrace();
}
无法工作,因为当您的代码到达if (x == Id) {
x
时仍然是-1
,因为该计划无法响应任何关键事件......
您也可以查看How to Use Text Fields和How to Write an Action Listeners