简体   繁体   English

从另一个类在 JTextArea 中键入文本

[英]Type a text in JTextArea from another class

I have a problem with updating a textArea from another class.我在更新另一个类的 textArea 时遇到问题。 I need a textArea to show a text while pressing a button.我需要一个 textArea 来在按下按钮时显示文本。 So when I press a buton I make a method actionPerformed() in ParceListener to print a text in a textArea which is located in MainFormAppearance class.因此,当我按下按钮时,我在 ParceListener 中创建了一个方法 actionPerformed() 以在位于 MainFormAppearance 类中的 textArea 中打印文本。 But it doesn't do that.但它不会那样做。 Could you please help me?请你帮助我好吗?

public class Main {

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame jFrame = new JFrame("Title");

    MainFormAppearance demo = new MainFormAppearance();
    jFrame.setContentPane(demo.createContentPanel());

    jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
    jFrame.setSize(400,300);
    jFrame.setVisible(true);

}
}

MainFormAppearance主窗体外观

package com.company;

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

public class MainFormAppearance {

public JPanel totalGui;
public JTextArea frame;
public JLabel blueLabel;
public JButton parceButton;
public JButton mailButton;
public ParceListener parceListener;

public JPanel createContentPanel(){

    totalGui = new JPanel();
    frame = new JTextArea();
    blueLabel = new JLabel("Some program");
    parceButton = new JButton("Button 1");
    mailButton = new JButton("Button 2");
    parceListener = new ParceListener();
    totalGui.setLayout(null);

    //set program window
    blueLabel.setLocation(10,10);
    blueLabel.setSize(400,20);
    blueLabel.setHorizontalAlignment(SwingConstants.CENTER);
    blueLabel.setForeground(Color.blue);
    totalGui.add(blueLabel);

    //set Button 1
    parceButton.setLocation(270, 50);
    parceButton.setSize(100,30);
    totalGui.add(parceButton);
    //Pressing the Button 1
    parceButton.addActionListener(parceListener);

    //set Button 2
    mailButton.setLocation(270, 100);
    mailButton.setSize(100, 30);
    totalGui.add(mailButton);

    frame.setLocation(20, 115);
    frame.setSize(200, 15);
    totalGui.add(frame);

    totalGui.setOpaque(true);
    return totalGui;
}

public void setTextArea(String myString){
    frame.append(myString);
}


}

ParceListener包监听器

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ParceListener implements ActionListener {

public String text = "some text";
MainFormAppearance mainFormAppearance = new MainFormAppearance();

public void actionPerformed(ActionEvent e) {
    mainFormAppearance.setTextArea(text);

}

}

It shows NullPointerException at frame.append(myString);它在 frame.append(myString); 处显示 NullPointerException in MainFormAppearance class.在 MainFormAppearance 类中。

Calling MainFormAppearance mainFormAppearance = new MainFormAppearance();调用MainFormAppearance mainFormAppearance = new MainFormAppearance(); in your ParceListener is creating a new instance of MainFormAppearance which has nothing to do with what is actually been presented on the screen.在您的ParceListener中创建MainFormAppearance一个新实例,它与屏幕上实际显示的内容无关。

You need some way to return information back to the main UI from ParceListener .您需要某种方式将信息从ParceListener返回到主 UI。

This is best accomplished using an Observer Pattern, where ParceListener generates notifications/events when something changes.这最好使用观察者模式来完成,其中ParceListener在发生变化时生成通知/事件。 It shouldn't care about "who" is interested, only that they are.它不应该关心“谁”感兴趣,只关心他们。

Let's start with a simple interface...让我们从一个简单的界面开始......

public interface ParceObserver {
    public void parceChanged(String text);
}

MainFormAppearance can now implement this interface and make what ever updates it needs. MainFormAppearance现在可以实现此interface并进行所需的更新。

public class MainFormAppearance implements ParceObserver {
    //...

    public void parceChanged(String text) {
        frame.append("\n" + text);
    }
}

Then thing here is, ParceListener , doesn't care what happens after it's posted the notification.那么这里的事情是, ParceListener ,不在乎发布通知后会发生什么。

Now, you just need to pass an instance of ParceObserver to ParceListener现在,您只需要将ParceListener一个实例ParceObserverParceListener

parceListener = new ParceListener(this);

And update ParceListener to make use of it...并更新ParceListener以使用它...

public class ParceListener implements ActionListener {

    private ParceObserver observer;

    public String text = "some text";

    public ParceListener(ParceObserver observer) {
        this.observer = observer;
    }

    public void actionPerformed(ActionEvent e) {
        if (observer == null) {
           return null;
        }
        observer.parceChanged(text);
    }

}

Now it's nicely de-coupled and re-usable.现在它很好地解耦并且可以重用。

And, if someone tells you to just pass a reference of the JTextArea or MainFormAppearance to ParceListener , please don't listen to them.而且,如果有人告诉您只将JTextAreaMainFormAppearance的引用传递给ParceListener ,请不要听他们的。 It's inappropriate, tightly couples your code and exposes the components to the risk of been modified in ways you never intended them to be这是不合适的,紧密耦合您的代码并使组件面临以您从未希望它们被修改的方式被修改的风险

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

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