Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何将内部类中的变量分配给外部类/或任何其他类中的变量?_Java_String_Swing_Inner Classes - Fatal编程技术网

Java-如何将内部类中的变量分配给外部类/或任何其他类中的变量?

Java-如何将内部类中的变量分配给外部类/或任何其他类中的变量?,java,string,swing,inner-classes,Java,String,Swing,Inner Classes,我有一个包含textfield的类,在该类中有一个包含action performed方法的内部类。输入文本并按enter键后,我希望将文本分配给实现Action Listener的内部类之外的字符串“s”。我想在另一个类中使用该字符串 public class Peaches extends JFrame { private JTextField item1; public Peaches() { super("the title"); set

我有一个包含textfield的类,在该类中有一个包含action performed方法的内部类。输入文本并按enter键后,我希望将文本分配给实现Action Listener的内部类之外的字符串“s”。我想在另一个类中使用该字符串

public class Peaches extends JFrame {
    private JTextField item1;

    public Peaches() {
        super("the title");
        setLayout(new FlowLayout());        
        item1 = new JTextField(10);
        add(item1);

    thehandler handler = new thehandler(); //object of thehandler inner class
    item1.addActionListener(handler);      

    // I want to use the textfield content here, or in other classes, updated
    // with the new text everytime i hit enter after entering new text in the textfield 

    //String s = the content of the textfield
}

private class thehandler implements ActionListener { // inner class
    @Override
    public void actionPerformed(ActionEvent event) {
        String string = "";           
        string = String.format(event.getActionCommand());            
        JOptionPane.showMessageDialog(null, string);           
    }
}
有没有办法做到这一点?必须能够在程序的其他部分使用文本字段的输入。我希望我能说清楚,谢谢大家

编辑
感谢您的回复,因此将变量字符串设置为类变量而不是在构造函数中声明它可以让整个类访问它,这很简单,但我不理解。感谢Marko,如果您将s移动为类变量而不是方法变量(正如当前一样,在构造函数中注释掉),它将起作用

如果将s移动为类变量而不是方法变量(正如当前一样,在构造函数中注释掉),它将起作用

您可以将外部类作为参数传递给内部类的构造函数。

您可以将外部类作为参数传递给内部类的构造函数。

我建议您这样做:

public class Peaches extends JFrame {
  private JTextField item1;
  private String string;

  public Peaches() {
    super("the title");
    setLayout(new FlowLayout());
    item1 = new JTextField(10);
    add(item1);
    item1.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent event) {
        string = String.format(event.getActionCommand() );
        JOptionPane.showMessageDialog(null, string);
    }});
  }
}

这使用了一个匿名类,比周围有几十个内部类更简单。此外,您还可以使用这些闭包(使用在匿名类之外声明的局部变量)。您的字符串在外面,因此您可以访问它。

我建议您这样做:

public class Peaches extends JFrame {
  private JTextField item1;
  private String string;

  public Peaches() {
    super("the title");
    setLayout(new FlowLayout());
    item1 = new JTextField(10);
    add(item1);
    item1.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent event) {
        string = String.format(event.getActionCommand() );
        JOptionPane.showMessageDialog(null, string);
    }});
  }
}
这使用了一个匿名类,比周围有几十个内部类更简单。此外,您还可以使用这些闭包(使用在匿名类之外声明的局部变量)。你的字符串在外面,所以你可以访问它。

我想在这里使用textfield内容,或者在其他类中,每次在textfield中输入新文本后按enter键时,都会使用新文本更新

这里的简短回答是,如果希望每次更改textfield中的值时都执行代码,则必须将侦听器附加到textfield并通过侦听器触发该代码。这就是听众的目的。。。唯一知道文本已更改的是textfield,他将通知其侦听器已进行更改的事实()

如果您想在多个课程中了解此更改,您可以

  • 提供API将侦听器从当前类外部附加到此特定文本字段
  • 提供API以允许从当前类外部直接访问textfield
  • 将一个侦听器附加到更新某种模型的textfield,并在不同的类之间共享该模型。当然,模型应该能够触发事件并将更改告知侦听器,否则您将无法判断值何时更改
是否使用内部类、匿名类、完整类作为侦听器并不重要。

我想在这里使用textfield内容,或者在其他类中使用textfield内容,每次在textfield中输入新文本后按enter键,都会用新文本进行更新

这里的简短回答是,如果希望每次更改textfield中的值时都执行代码,则必须将侦听器附加到textfield并通过侦听器触发该代码。这就是听众的目的。。。唯一知道文本已更改的是textfield,他将通知其侦听器已进行更改的事实()

如果您想在多个课程中了解此更改,您可以

  • 提供API将侦听器从当前类外部附加到此特定文本字段
  • 提供API以允许从当前类外部直接访问textfield
  • 将一个侦听器附加到更新某种模型的textfield,并在不同的类之间共享该模型。当然,模型应该能够触发事件并将更改告知侦听器,否则您将无法判断值何时更改

是否使用内部类、匿名类和成熟类作为侦听器并不重要。

您根本不需要内部类。只需在Peaches中实现一个ActionListener,并让它监听
ActionEvent

public class Peaches extends JFrame implements ActionListener
{
    private JTextField item1;

    public Peaches()
    {
        super("the title");
        setLayout(new FlowLayout());        
        item1 = new JTextField(10);
        add(item1);

        item1.addActionListener(this);      
    }

    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == item1){
            String string = "";           
            string = String.format(event.getActionCommand() );            
            JOptionPane.showMessageDialog(null, string);   
        }        
    }
}

您根本不需要内部类。只需在Peaches中实现一个ActionListener,并让它监听
ActionEvent

public class Peaches extends JFrame implements ActionListener
{
    private JTextField item1;

    public Peaches()
    {
        super("the title");
        setLayout(new FlowLayout());        
        item1 = new JTextField(10);
        add(item1);

        item1.addActionListener(this);      
    }

    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == item1){
            String string = "";           
            string = String.format(event.getActionCommand() );            
            JOptionPane.showMessageDialog(null, string);   
        }        
    }
}

一个实例变量,我会说。一个实例变量,我会说。这有什么帮助?没有理由让它成为一个成员变量,因为我可以调用
item1.getText()
,所以在整个类中已经可以访问该
字符串了Java仍然不支持闭包,匿名类也不能解决这个问题。您无法访问局部变量,只能访问
final
变量。这与真正支持闭包的情况不同,能够访问最终var要比不能访问任何var好得多。闭包的要点是在词法范围上闭包——这就是我们在Java中所做的。易变性可以通过单元素数组的标准技巧实现。顺便说一句,闭包来自Scheme或Clojure等FP语言。你认为这些语言允许访问非最终变量吗?这有什么帮助?没有理由让它成为一个成员变量,因为我可以调用
item1.getText()
,所以在整个类中已经可以访问该
字符串了Java仍然不支持闭包,匿名类也不能解决这个问题。您无法访问局部变量,只能访问
final
变量。这与真正支持闭包的情况不同,能够访问最终var比不能访问要好得多