为什么我的 Java 代码无法获取 JText 字段的值?

发布于 2024-12-11 09:00:20 字数 1678 浏览 0 评论 0原文

我试图从文本字段(由用户输入)获取一个值以用于处理。但无论我做什么,它都不会得到输入的值,它似乎仍然是空的。有人可以告诉我为什么它无法从文本字段获取值吗?

这是最初创建名为 writeStrings 的文本字段的方法

public void chooseEmpToAdd()
{
     JTextArea EmpDetails = new JTextArea(5,20);
     JTextField writeStrings  = new JTextField(20); 
     JLabel enterIDno = new JLabel("Please enter The Employye ID number that you wish to assign to a department: ");  

     JButton  submit  = new JButton  (" Submit") ;
       ButtonListenerEmp Listener2 = new ButtonListenerEmp(); 
      submit.addActionListener(Listener2);
     JFrame frameAllEmps = new JFrame();
      frameAllEmps.setSize( 150, 140 );
     frameAllEmps.pack();
     frameAllEmps.setVisible(true); 

     //layout
     frameAllEmps.setLayout(new FlowLayout());
      frameAllEmps.add(enterIDno);

    int x = 0;
    System.out.println("ALL Emps from the tree map");
    for(int key:employeeMap.keySet())
    {

        Employee dEmp = employeeMap.get(key);
        System.out.println("Employe no :" +x+": "+dEmp);
        EmpDetails.setText(EmpDetails.getText()+" "+dEmp);
        frameAllEmps.add(EmpDetails);   
         x++;
    }

    frameAllEmps.add(new JScrollPane(EmpDetails));
    frameAllEmps.add(writeStrings);
    frameAllEmps.add(submit);
    frameAllEmps.pack();

}

,这是应该从文本框中获取值并将其打印到控制台的操作侦听器,但它不起作用。

 private class ButtonListenerEmp implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
            String ID ; 
            int dID;


               ID = writeStrings.getText();
                System.out.println("start of try b4 changes: "+ID);     
               }
           }

Im trying to get a value from a text field (entered by the users) to use for processing. But no matter what I do it will not get the value that was entered it seem to remain empty. Could some one please tell me why it will not get the value from the Text field.

this is the method which initialy created the text field which was named writeStrings

public void chooseEmpToAdd()
{
     JTextArea EmpDetails = new JTextArea(5,20);
     JTextField writeStrings  = new JTextField(20); 
     JLabel enterIDno = new JLabel("Please enter The Employye ID number that you wish to assign to a department: ");  

     JButton  submit  = new JButton  (" Submit") ;
       ButtonListenerEmp Listener2 = new ButtonListenerEmp(); 
      submit.addActionListener(Listener2);
     JFrame frameAllEmps = new JFrame();
      frameAllEmps.setSize( 150, 140 );
     frameAllEmps.pack();
     frameAllEmps.setVisible(true); 

     //layout
     frameAllEmps.setLayout(new FlowLayout());
      frameAllEmps.add(enterIDno);

    int x = 0;
    System.out.println("ALL Emps from the tree map");
    for(int key:employeeMap.keySet())
    {

        Employee dEmp = employeeMap.get(key);
        System.out.println("Employe no :" +x+": "+dEmp);
        EmpDetails.setText(EmpDetails.getText()+" "+dEmp);
        frameAllEmps.add(EmpDetails);   
         x++;
    }

    frameAllEmps.add(new JScrollPane(EmpDetails));
    frameAllEmps.add(writeStrings);
    frameAllEmps.add(submit);
    frameAllEmps.pack();

}

And this is the action listener which should take the value from the Text box and print it to the console, but it does not work.

 private class ButtonListenerEmp implements ActionListener
    {
        public void actionPerformed (ActionEvent e )
        {
            String ID ; 
            int dID;


               ID = writeStrings.getText();
                System.out.println("start of try b4 changes: "+ID);     
               }
           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵性 2024-12-18 09:00:21

监听器实现不应该访问局部变量writeStrings,我什至不确定它将如何编译——您发布的代码准确吗?

哦,您可能同时拥有一个局部变量 writeStrings 一个实例变量 writeStrings,尽管很难说,因为您没有发布其余代码。尝试不要在 chooseEmpToAdd 方法中声明 writeStrings;请改用类变量。

The listener implementation shouldn't have access to the local variable writeStrings, I'm not even sure how that would compile--is the code you posted accurate?

Oh, you probably have both a local variable writeStrings, and an instance variable writeStrings, although it's hard to say since you didn't post the rest of the code. Try not declaring writeStrings in the chooseEmpToAdd method; use the class variable instead.

寒冷纷飞旳雪 2024-12-18 09:00:21

因为您将文本字段声明为 chooseEmpToAdd 方法的局部变量,所以 ButtonListenerEmp 类看不到它。要解决此问题,请将文本字段声明为类字段并将其公开或将文本字段的文本作为 ButtonListenerEmp 构造函数的参数传递。

Because you're declaring the text field as a local variable of chooseEmpToAdd method, so it is not being seen by ButtonListenerEmp class. To solve this problem, declare the text field as a class field and make it public or pass the text of the text field as an argument of ButtonListenerEmp constructor.

笛声青案梦长安 2024-12-18 09:00:21

变量 ID 可能尚未初始化(即 String ID = "";)。

另外,在 writeStrings.getText() 行处存在编译错误,因为变量 writeStrings 不存在于 chooseEmpToAdd() 方法之外。尝试在该方法之前声明 JTextField writeStrings = new JTextField(20);

The variable ID might not have been initialized (i.e. String ID = "";)

Also there is a compile error at the line writeStrings.getText() because the variable writeStrings doesn't exist outside of the chooseEmpToAdd() method. Try declaring JTextField writeStrings = new JTextField(20); before the method instead.

初心未许 2024-12-18 09:00:21

从所提供的代码中我想到的一件事是,您创建新的 JTextField 并将其存储在名为 writeStrings本地变量中,也许与你稍后尝试阅读的不一样。

One thing that comes to my mind from the code presented is that you create the new JTextField and store it in a local variable called writeStrings maybe that is not the same you try to read from lateron.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文