[英]How I can get output from 1st frame textfield input text to 2nd frame textArea
[英]How can I add a TextArea from a returned ArrayList to a frame?
我試圖返回一個 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;
}
}
執行該方法並獲取文本區域的代碼片段:
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;
我添加處理程序的代碼片段:
newFile.addActionListener(mainMenuHandler);
你為什么使用 AWT。 您至少應該使用 Swing。
MainMenu mainMenu = new MainMenu();
mainMenu.add((TextArea)newFile().get(1));
在您的情況下,您將文本區域添加到 MainMenu,但您從未將 MainMenu 添加到框架中。
因此,您需要將“mainMenu”添加到框架中。
此外,當使用 Swing 時,如果將組件添加到可見框架,則需要在添加組件的容器上調用revalidate()
和repaint()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.