简体   繁体   English

如何将返回的 ArrayList 中的 TextArea 添加到框架中?

[英]How can I add a TextArea from a returned ArrayList to a frame?

I am trying to return an ArrayList with two values, text and a text area.我试图返回一个 ArrayList 有两个值,文本和文本区域。 However when I press the button that is meant to do this, nothing happens.但是,当我按下本应执行此操作的按钮时,什么也没有发生。 Can you please explain why this is the case?你能解释一下为什么会这样吗?

The method that returns the ArrayList:返回 ArrayList 的方法:

public ArrayList<Object> newFile(){
      ArrayList<Object> objectArrayList = new ArrayList<>();
      TextArea textField = new TextArea("Type File Information.., ");
      AtomicReference<String> fileText = new AtomicReference<>();


      textField.addTextListener((textListener) -> {
          fileText.set(textField.getText());
      });
      objectArrayList.add(String.valueOf(fileText));
      objectArrayList.add(textField);

      return objectArrayList;
    }
}

The code snippet that executes the method and gets the text area:执行该方法并获取文本区域的代码片段:

public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();

        switch (action) {
            case "New":
                fileText = (String) newFile().get(0);
                MainMenu mainMenu = new MainMenu();
                mainMenu.add((TextArea)newFile().get(1));
                break;

The code snippet where I add the handler:我添加处理程序的代码片段:

newFile.addActionListener(mainMenuHandler);

Why are you using AWT.你为什么使用 AWT。 You should at least be using Swing.您至少应该使用 Swing。

MainMenu mainMenu = new MainMenu();
mainMenu.add((TextArea)newFile().get(1));

In your case you add the text area to the MainMenu, but you never add the MainMenu to the frame.在您的情况下,您将文本区域添加到 MainMenu,但您从未将 MainMenu 添加到框架中。

So you would need to add the "mainMenu" to the frame.因此,您需要将“mainMenu”添加到框架中。

Also, when using Swing if you add a component to a visible frame then you need to invoke revalidate() and repaint() on the container you add the component to.此外,当使用 Swing 时,如果将组件添加到可见框架,则需要在添加组件的容器上调用revalidate()repaint()

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

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