简体   繁体   English

如何从类中添加匿名实例作为ActionListener

[英]How to add an anonymous instance off a class as an ActionListener

Im working on a lab that requires me to make a JFrame with 2 inner classes. 我在一个需要我制作带有2个内部类的JFrame的实验室中工作。 One that entends JPanel, has a text area and a jbutton. 带有JPanel的控件有一个文本区域和一个jbutton。 And another that implements action listener. 另一个实现动作侦听器。 How do i add an anonymouse instance of the second class to my JButton that is already in an inner class. 如何将第二个类的匿名实例添加到已经在内部类中的JButton中。 Here is the brief to get a better understanding. 这里是简要了解的内容。

在此处输入图片说明

here is the code i have written so far. 这是我到目前为止编写的代码。 I can get the Frame to appear, but the JPanel doesnt appear, nor does the JButtons or JTextArea. 我可以使Frame出现,但是JPanel不会出现,JButtons或JTextArea也不会出现。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;



@SuppressWarnings("serial")
public class FormFrame extends JFrame
{

public static void main(String[] args)
{
    JFrame frame = new FormFrame();
    frame.setLocationRelativeTo(null);
}   
public FormFrame()
{
    Container contentPane = getContentPane();

    RegisterPanel p = new RegisterPanel();
    p.button.addActionListener(new SubmitResponder());
    //
    // Here is where im lost...
    //


    contentPane.add(p);
    setSize(300, 200);
    setVisible(true);
}

class RegisterPanel extends JPanel
{

    JPanel panel = new JPanel();
    JTextField text = new JTextField();
    JButton button = new JButton("Submit");     
}

class SubmitResponder implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()== RegisterPanel.button) //Asks me to make button static here
        {

            //Shows "No enclosing instance of the type FormFrame.RegisterPanel is accessible in scope"
                RegisterPanel.this.text.setText("Submit Complete");

        }
    }
}
}

Any help with this would be much appreciated 任何帮助,将不胜感激

You could pass the RegisterPanel instance to the action listener: 您可以将RegisterPanel实例传递给操作侦听器:

class SubmitResponder implements ActionListener {

    private final RegisterPanel rp;

    public SubmitResponder(RegisterPanel rp) {
        this.rp = rp;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        rp.text.setText("Submit Complete");
    }
}

There's no need to check the source btw. 无需检查源代码。 The AL is only listening to 1 source. AL仅收听1个源。

RegisterPanel p = new RegisterPanel();
p.button.addActionListener(new SubmitResponder(p));
p.button.addActionListener(new SubmitResponder());

Here the SubmitResponder is already an anonymous instance, quite literally, because it has no name. 实际上, SubmitResponder已经是一个匿名实例,因为它没有名称。

Your error about "no enclosing instance" is unrelated. 您与“无封闭实例”有关的错误无关。 Since SubmitResponder is not an inner class of RegisterPanel (it's a sibling) it doesn't belong to an instance of RegisterPanel and so it cannot logically refer to RegisterPanel.this . 由于SubmitResponder不是RegisterPanel的内部类(它是同级兄弟),它不属于RegisterPanel的实例,因此不能在逻辑上引用RegisterPanel.this How would it know which instance that is? 它怎么知道是哪个实例? There might be many, or even zero, depending on how many the parent FormFrame decides to create. 取决于父FormFrame决定创建多少个,可能有很多甚至是零。 It so happens that there's only one, but that's not the point. 碰巧只有一个,但这不是重点。 On the other hand if you said FormFrame.this there would be no doubt what that meant no matter the code did, unless RegisterPanel stopped being an inner class or it became static. 另一方面,如果您说的是FormFrame.this ,那么无论代码做什么,毫无疑问这意味着什么,除非RegisterPanel不再是内部类或它成为静态类。 Does that make sense? 那有意义吗?

To do what you want, the SubmitResponder needs to talk to RegisterPanel via a method in FormFrame . 若要执行您想要的操作, SubmitResponder需要通过FormFrame的方法与RegisterPanel进行FormFrame Incidentally you don't actually need to say FormFrame.this.doSomething() unless SubmitResponder also has a method called doSomething . 顺便说FormFrame.this.doSomething()除非SubmitResponder也有一个名为doSomething的方法,否则您实际上不需要说FormFrame.this.doSomething()

The instructions tell you that the RegisterPanel should be a field in the FormFrame class, which you haven't done. 这些说明告诉您RegisterPanel应该是FormFrame类中的一个字段,您尚未完成。 Something like this: 像这样:

public class FormFrame extends JFrame
{
    RegisterPanel panel;
    ...

    public FormFrame()
    {
        panel = new RegisterPanel();
        ...
    }

    ...

    class SubmitResponder implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == panel.button)
            {
                panel.text.setText(...);
            }
        }
    }
}

Now you can access panel from inside the SubmitResponder class. 现在,您可以从SubmitResponder类内部访问panel


As a side note, the instructions are using some terminology in an ambiguous and incorrect way: 附带说明一下,这些指令以一种模棱两可和错误的方式使用了一些术语:

  • "Anonymous instance" is not an official term with a precise meaning. “匿名实例”不是具有确切含义的正式术语。
  • Using official definitions, "class field" would imply the static modifier. 使用官方定义,“类字段”将隐含static修饰符。 Given the context of the assignment, I doubt that's correct. 考虑到任务的背景,我怀疑这是正确的。 It should probably have said "instance field". 它可能应该说“实例字段”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM