JTextArea总是null?

Cip*_*hor 3 java swing jframe jbutton jtextarea

这是我的代码,这是非常简单,它只是创造了JFrame一个JTextArea在中心.

if(!txtSource.getText().trim().equals("") && txtSource != null)

即使我在JTextArea中输入了文本,也永远不会满足.

如果JTextArea有一些文本,我只想执行methodA().

private Container content;
private JTextArea txtSource;

public Test() {
    this.setTitle("Test");
    this.setSize(600,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    content = this.getContentPane();
    content.add(getTextArea(), BorderLayout.CENTER);
    content.add(button(), BorderLayout.SOUTH);
    this.setVisible(true); 
}

private JTextArea getTextArea() {
    JTextArea txtSource = new JTextArea(20, 80);
    return txtSource;
}

private JButton button() {

    JButton btn = new JButton("Click me");

    btn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             if(!txtSource.getText().trim().equals("") && txtSource != null) {
                 methodA();
             } else { 
                 System.out.println("Please paste your script in.");
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

请帮帮我......

Rei*_*eus 7

遮蔽txtSource变量,更换

JTextArea txtSource = new JTextArea(20, 80);
Run Code Online (Sandbox Code Playgroud)

txtSource = new JTextArea(20, 80);
Run Code Online (Sandbox Code Playgroud)